September 24, 2026
Why file10 sorts before file2 (and how natural sorting fixes it)
Sort a list of files named file1, file2, and so on up to file10, and a basic sort gives you file1, file10, file2, file3. It looks like a bug, but the computer is doing exactly what it was asked. Understanding why explains a lot about how sorting works, and when to use natural sorting instead.
How alphabetical sorting works
Standard text sorting compares two strings one character at a time, from left to right. As soon as two characters differ, that position decides the order. Comparing "file10" and "file2", the first four characters match, and then it compares "1" with "2". Since "1" comes before "2", "file10" goes first. The sort never looks at the whole number 10; it only sees the character "1".
This is called lexicographic order, and it's how dictionaries work too. It's perfectly correct for words, where you want "car" before "cart" before "cat". It just doesn't match human expectations for numbers embedded in text.
Natural sorting
Natural sort (sometimes called human sort or alphanumeric sort) treats a run of digits as a single number. It compares "file" with "file", then compares 10 with 2 as numbers, and correctly places file2 first. The result matches how people read: file1, file2, file3, up to file10.
Most modern file managers, including Windows Explorer and macOS Finder, sort names naturally by default. Many command-line tools and programming languages sort lexicographically unless you ask otherwise.
The zero-padding workaround
Before natural sorting was common, the standard fix was zero-padding numbers to the same width: file01, file02, up to file10. Because every number has the same number of digits, lexicographic order and numeric order agree. It's still a good habit for file names you'll share, since it sorts correctly everywhere, and it's why dates are often written as 2026-09-24: year-month-day with padded numbers sorts chronologically as plain text.
Case and accents
Sorting also has to decide what to do with capital letters and accented characters. In raw character-code order, every uppercase letter comes before every lowercase letter, so "Zebra" sorts before "apple". Language-aware sorting, which is what browsers provide, fixes that and places "é" next to "e" rather than after "z".
Rules also vary by language. In Swedish, for example, "ä" is sorted after "z", while in German it's usually treated like "a". A locale-aware sort follows the conventions of a specific language.
Sorting numbers that aren't in text
If each line is just a number, including decimals or negatives, natural sort usually handles it well. But be careful with numbers formatted with thousands separators or currency symbols, and with negative numbers, since a leading minus sign is treated as a character. For serious numeric data, a spreadsheet's numeric sort is more reliable.
Other list tasks
Sorting is often combined with other cleanup: trimming stray spaces, removing blank lines, and removing duplicates. The order matters. Trim first, so that "apple" and "apple " count as the same line, then remove duplicates, then sort.
The line sorter and deduplicator offers both standard A to Z sorting and natural sorting, along with deduplication, blank-line removal, trimming, reversing, and a fair random shuffle, all applied in your browser.
Want to try it yourself?
Open the Line Sorter and Deduplicator →