Skip to main content

sorting a php array alphanumerically


I know there are natsort() and natcasesort() functions in php to sort array elements in natural order. I am trying to sort the following items.




array[0]= '10. xyz';
array[1]= '13. xyz';
array[2]= '2. xyz';
array[3]= '1a. xyz';
array[4]= '6. xyz';
array[5]= '1b. xyz';
array[6]= '4a. xyz';
array[7]= '4b. xyz';



now if I apply natsort(), '10..' and '13..' are placed above '1a..'. how can I make my php code to think that '10..' is greater than '1a...' and '1a...' should be at the top? the correct output in my particular case is:




'1a. xyz';
'1b. xyz';
'2. xyz';
'4a. xyz';
'4b. xyz';
'6. xyz';
'10. xyz';
'13. xyz';



i used a custom algo but it does the same...it places '10..' before '1a...'. heres my custom func:




function cmp($a, $b) {
$a_ex=explode(".",$a);
$b_ex=explode(".",$b);
if ($a_ex[0] == $b_ex[0] ) {
return 0;
}
return ($a_ex[0] < $b_ex[0]) ? -1 : 1;
}


Source: Tips4allCCNA FINAL EXAM

Comments

  1. Use natsort():

    $array[0]= '10. xyz';
    $array[1]= '13. xyz';
    $array[2]= '2. xyz';
    $array[3]= '1a. xyz';
    $array[4]= '6. xyz';
    $array[5]= '1b. xyz';
    $array[6]= '4a. xyz';
    $array[7]= '4b. xyz';

    natsort($array);

    print_r($array);

    ReplyDelete
  2. natsort works fine for me...

    Otherwise use str_pad to format the numbers before you sort the array.

    Or you can user usort with a custom function, maybe with intval...

    ReplyDelete
  3. use usort with your own custom algorithm

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.