Home > Articles


  • Print
  • + Share This

3.14 Sorting Sensibly

You want to make your program sort a list like a human would, where the number 10 would come after the number 2.


Use the natsort() function:

$files = array("file001.txt", "file002.txt", "file003.txt",
        "file010.txt", "file023.txt", "file004.txt");
foreach ($files as $file) {
  print "$file\n";


The natsort() function uses a sorting algorithm that is described by Martin Pool at http://www.linuxcare.com.au/projects/natsort/. A case-insensitive version of the natsort() function is also available, natcasesort().

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.