Main Content
PHP and natural sorting Archive - Originally posted on "The Horse's Mouth" - 2004-09-19 10:01:47 - Graham Ellis
If you sort (8, 9, 10, 11, 12), how do you want them to be output? "In the order you've just listed, Graham", you'll probably say. But if they're strings and you sort them, then you'll get the order 10, 11, 12, 8, 9 output if you just use a default sort, because the character "1" comes before the character "8" in the alphabet.
Using PHP, you could write your own sort routine (using usort) ... but there's a better way - a function called "natsort". Remember the PHP rule of thumb - if you think surely someone's done that before then often the solution will be there's a function to do that .
Here's an example of the output from an illustrative program:
Original
0 .. 12 Drummers Drumming
1 .. 7 Swans a'Swimming
2 .. 11 Pipers Piping
3 .. 9 Ladies Dancing
4 .. 10 Lords a'Leaping
5 .. 8 Maid a'Milking
Default Sort
0 .. 10 Lords a'Leaping
1 .. 11 Pipers Piping
2 .. 12 Drummers Drumming
3 .. 7 Swans a'Swimming
4 .. 8 Maid a'Milking
5 .. 9 Ladies Dancing
Numeric sort
0 .. 7
1 .. 8
2 .. 9
3 .. 10
4 .. 11
5 .. 12
Natural Sort
1 .. 7 Swans a'Swimming
5 .. 8 Maid a'Milking
3 .. 9 Ladies Dancing
4 .. 10 Lords a'Leaping
2 .. 11 Pipers Piping
0 .. 12 Drummers Drumming
This program can be run from here and you can view the source code here .
Some other articles
H999 - Additional PHP Material Image indexer / thumbnail display scripts in PHP Catchable fatal error in PHP ... How to catch, and alternative solutions such as JSON Arrays of arrays - or 2D arrays. How to program tables. Exception handling in PHP If nothing, make it nothing. Extra PHP Examples PHP Techniques - a workshop Flipping images on your web page Script to present commonly used images - PHP Copyright and theft of images, bandwidth and members. More PHP sample and demonstration programs Ordnance Survey Grid Reference to Latitude / Longitude Converting from postal address to latitude / longitude Controlling and labelling Google maps via PHP PHP Standalone - keyboard to screen Drawing hands on a clock face - PHP Drawing dynamic graphs in PHP Sorting people by name in PHP Parallel processing in PHP Dates, times, clickable diarys in PHP Display an image from a MySQL database in a web page via PHP Syntax checking in PHP Reporting on the 10 largest files or 10 top scores PHP - a team member leaves Check your user is human. Have him retype a word in a graphic Hot answers in PHP Presentation, Business and Persistence layers in Perl and PHP PHP Image viewing application PHP - setting sort order with an associative array Merging pictures using PHP and GD Running a Perl script within a PHP page Double Dollars in PHP Stand alone PHP programs Time calculation in PHP the array returned by preg_match_all More maps Ordnance Survey - using a 'Get a map' What and why for the epoch This article H107 - String Handling in PHP Splitting the difference with PHP Setting up strings in PHP Solution looking for a problem? Lookahead and Lookbehind More than just matching with a regular expression in PHP Getting more than a yes / no answer from a regular expression pattern match Learning to program in PHP - Regular Expression and Associative Array examples Regular Expression modifiers in PHP - summary table PHP - moving from ereg to preg for regular expressions Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP Handling (expanding) tabs in PHP Curly braces within double quoted strings in PHP Handling nasty characters - Perl, PHP, Python, Tcl, Lua Making Regular Expressions easy to read and maintain Finding variations on a surname Regular Expressions in PHP Regular expression for 6 digits OR 25 digits Do not SHOUT and do not whisper Short and sweet and sticky - PHP form input A taster PHP expression ... Ignore case in Regular Expression Regular Express Primer PHP Regular expression to extrtact link and text Date conversion - PHP Looking ahead and behind in a Regular Expression Evaluating arithmetic expressions in configuration files How similar are two words Don't expose your regular expressions Robust PHP user inputs PHP - dividing a string up into pieces The fencepost problem Converting between acres and hectares Running a Perl script within a PHP page Splitting the difference PHP Magic Quotes the array returned by preg_match_all This article Here documents