Natsort example

Shows the advantages of natsort() in comparison to sort().

// create a example array
$array_1 = array(
    'pic10.jpg', 'pic3.jpg', 'pic1.jpg', 
    'pic7.jpg', 'pic99.jpg', 'pic127.jpg'
);
 
// copy array 1
$array_2 = $array_1;
 
 
sort($array_1);
 
print "Standard sorting algorithm:<br/>\n";
print_r($array_1);
print "\n\n";
 
 
natsort($array_2);
 
print "Natural order sorting:<br/>\n";
print_r($array_2);
 
/*
Output:
 
Standard sorting algorithm
Array
(
    [0] => pic1.jpg
    [1] => pic10.jpg
    [2] => pic127.jpg
    [3] => pic3.jpg
    [4] => pic7.jpg
    [5] => pic99.jpg
)
 
 
Natural order sorting
Array
(
    [2] => pic1.jpg
    [1] => pic3.jpg
    [3] => pic7.jpg
    [0] => pic10.jpg
    [4] => pic99.jpg
    [5] => pic127.jpg
)
*/
Snippet Details




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

None.