In computing, natural sort order (or natural sorting) is a way of ordering strings that treats embedded numbers as whole numerical values rather than sequences of characters.
While standard alphabetical order compares strings character-by-character (where "10" sorts before "2" because "1" is less than "2"), natural sort order orders them by magnitude of the number, placing "2" before "10".
Natural sort order is designed to address the shortcoming of standard lexicographical order, which often produces counter-intuitive results for humans when dealing with numbered lists, filenames, or version numbers. NaturalLexicographical1.jpg
2.jpg
3.jpg
9.jpg
10.jpg
11.jpg1.jpg 10.jpg
11.jpg
2.jpg
3.jpg
9.jpg
Problem with standard sorting In standard alphabetical (lexicographical) sorting, strings are compared character by character from left to right. This causes numbers to be sorted based on the value of their first digit, rather than their whole numerical value.
For example, a computer using standard sorting will place the string "11" before "2". This occurs because the character "1" (the first digit of 11) has a lower code value than "2". While mathematically correct in terms of character codes, this ordering disrupts the logical sequence expected by users, particularly in file management and data lists.
Operation Natural sorting algorithms generally operate by splitting strings into "chunks" of text and numbers. Text chunks are compared alphabetically (often case-insensitively). Numeric chunks are parsed into integer values and compared numerically.
Comparison of algorithms Standard Sorting (Lexicographical) Natural Sortingfile1.txt file10.txt
file11.txt
file12.txt
file2.txt
file20.txt
file3.txtfile1.txt file2.txt
file3.txt
file10.txt
file11.txt
file12.txt
file20.txt
Handling edge cases Different implementations of natural sort may handle edge cases differently: Leading zeros: Some algorithms treat "01" and "1" as identical, while others may enforce an ordering where "01" follows "1" (or vice versa) to ensure a deterministic sort. Whitespace: Most implementations ignore leading or trailing whitespace around the numbers to prevent sorting anomalies. Decimals and Version Numbers: A variation of natural sort, often called version sort, is designed to handle multiple numeric segments separated by dots (e.g., vs ). In standard sort, precedes ; in version sort, the segments are parsed individually, correctly placing after .
History and implementations Functionality to sort by natural sort order is now widely available in software libraries for many programming languages and operating systems.
The concept gained significant visibility in the Macintosh community. During the 1996 MacHack conference, the Natural Order Mac OS System Extension was conceived and implemented overnight as an entry for the Best Hack contest. Subsequently, Dave Koelle published the "Alphanum Algorithm" in 1997, a popular reference implementation that influenced many later libraries. Martin Pool published "Natural Order String Comparison" in 2000.
Modern implementations include: Dart: In the collection package there are many sort algorithms, and one of them is . PHP: The function is built into the standard library. Python: The library is a widely used third-party package. Perl: The module is available via CPAN. Unix/Linux: The GNU and commands support natural sorting via the (version sort) flag (for ) or the flag (for ). .NET/C#: Various extensions exist, such as .
File managers such as Windows Explorer (since Windows XP) and Midnight Commander utilize natural sorting by default to display file lists.
See also Collation Lexicographical order ISO 8601 Semantic versioning
References