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.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyYou 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);
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced Functions
Anonymous FunctionsClosures and 'use'Arrow FunctionsCallback FunctionsUsing 'call_user_func'Variable FunctionsPassing by ReferenceRecursive FunctionsRecap: Function Medley4Multi-dimensional Arrays
Creating a 2D ArrayAccessing 2D Array ElementsModifying 2D Array ElementsIterating with Nested Loops2D Associative ArraysRecap: Simple Grid Exercise2Advanced Array Manipulations
Adding with 'array_push'Removing with 'array_pop'Adding with 'array_unshift'Removing with 'array_shift'Merging Indexed ArraysMerging Associative ArraysExtracting with 'array_slice'Values with 'in_array'Keys with 'array_search'Recap: Playlist Exercise3Sorting Arrays
Sort Indexed Arrays AscendingSort Indexed Arrays DescendingSort Assoc Arrays by ValueSort Assoc Arrays by KeyNatural Order SortingCustom Sorting with 'usort'Recap: Leaderboard Sorting