What is Natural Order Sorting
As human beings when we are told to put a list in order, it is usually a pretty straightforward task. With computer programming, depending on how you want this list ordered, it isn't so straight cut.
For example, given a list below:
apple 2
apple 1
apple 12
If you were to pass this list into a computer, and tell it to sort this list you would end up with something like:
apple 1
apple 12
apple 2
The computer sorted these alphabetically, "apple 12" alphabetically comes before "apple 2", but not "logically".
There is another sorting algorithm that we can use to put this list into an order that makes more sense. This algorithm is known as the "natural order" algorithm. The natural order algorithm will yield a much more human friendly version of the sorted list:
apple 1
apple 2
apple 12
In php, respectively the normal sort function is sort() and the natural order sort function is natsort(). An example of the above situation below:
<?php
echo '<pre>';
$list = array(
'apple 2',
'apple 1',
'apple 12',
);
echo join("\n",$list). "\n\n";
sort($list);
echo join("\n",$list). "\n\n";
natsort($list);
echo join("\n",$list). "\n\n";
?>