Posted by & filed under PHP, Programming.

I recently had an issue where I was sorting a array using php’s sort() function. This worked fine, except the ordering gets out of logical order when you get into the double digits. Example:

1
2
20
21
3...

This is where natsort() comes in. This is PHP’s natural sorting algorithm. It sorts based on how a human would sort a list rather than the way a machine would.

So I replaced

sort($narray); 

with:

natsort($narray);
 $narray = array_values($narray);

Problem solved!

Leave a Reply

You must be logged in to post a comment.