Menu
Coddy logo textTech

Natural Order Sorting

Part of the Logic & Flow section of Coddy's PHP journey — lesson 24 of 68.

While PHP's standard sorting functions work well for most cases, they can produce unexpected results when sorting strings that contain numbers. The natsort() function solves this problem by using a "natural ordering" algorithm that sorts strings the way humans would naturally expect.

Consider this example with file names:

<?php
$files = ["file1.txt", "file10.txt", "file2.txt", "file20.txt"];

// Regular sort() produces: file1.txt, file10.txt, file2.txt, file20.txt
sort($files);
print_r($files);

// Reset the array
$files = ["file1.txt", "file10.txt", "file2.txt", "file20.txt"];

// natsort() produces: file1.txt, file2.txt, file10.txt, file20.txt
natsort($files);
print_r($files);
?>

The difference is crucial: regular sort() compares strings character by character, so "file10.txt" comes before "file2.txt" because "1" comes before "2" in ASCII. However, natsort() recognizes the numeric portions and sorts them numerically, placing "file2.txt" before "file10.txt" as you would naturally expect.

Like other PHP sorting functions, natsort() modifies the original array directly. This makes it particularly useful for organizing file listings, version numbers, or any data where strings contain embedded numbers that should be sorted numerically rather than alphabetically.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

challenge icon

Challenge

Easy

You will receive one input: an array of software version numbers in JSON format. Read the input, convert the JSON string to an array, use natsort() to sort the version numbers in natural order, and print the sorted array using print_r().

Input format: One line containing a JSON array of version strings (example: ["v1.10","v1.2","v1.20","v1.3"])

Expected output: The sorted array in natural order, displayed using print_r()

Cheat sheet

The natsort() function sorts strings using "natural ordering" - the way humans naturally expect strings with numbers to be sorted.

Regular sort() compares strings character by character, while natsort() recognizes numeric portions and sorts them numerically:

$files = ["file1.txt", "file10.txt", "file2.txt", "file20.txt"];

// Regular sort() produces: file1.txt, file10.txt, file2.txt, file20.txt
sort($files);

// natsort() produces: file1.txt, file2.txt, file10.txt, file20.txt
natsort($files);
print_r($files);

Like other PHP sorting functions, natsort() modifies the original array directly. It's particularly useful for file listings, version numbers, or any data with embedded numbers that should be sorted numerically.

Try it yourself

<?php
// Read input
$input = fgets(STDIN);

// Convert JSON string to array
$versions = (array)json_decode($input, true);

// TODO: Write your code below to sort the array using natsort()


// Output the sorted array
print_r($versions);
?>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow