fixmint

Why 10 sorts before 2, and how natural sorting fixes it

By · · 4 min read

A plain alphabetical sort compares the first character of each item, then the second, and so on. The first character of "10" is 1, and 1 comes before 2, so "10" sorts before "2". The order is correct for text and wrong for numbers. Natural sorting reads each run of digits as one whole number, so 2 comes before 10. Padding the numbers with zeros, such as 02 and 10, fixes the same problem in the names themselves.

Sort linesList is out of orderOpen

Why the computer does it this way

A plain sort compares text one position at a time, from the left, using character codes or the language's collation order. It stops at the first difference. That suits words: "apple" comes before "banana" because the first letters differ.

It fails with numbers. Take three file names: file-2, file-9 and file-10. In the last pair, the comparison reaches position 6, where the characters are 1 and 9. The 1 is smaller, so file-10 comes first. The plain order is file-10, file-2, file-9. That is correct for letters and wrong for anyone reading counts.

Natural sorting

Natural sorting, also called numeric or human sorting, splits each text into runs of digits and runs of other characters. It compares digit runs by their value and everything else as text. So 2 comes before 10, and file-2 comes before file-10. Many file managers use a sort like this by default, which is why a folder of numbered photos usually looks right in them.

Sort a list in fixmint

  1. Paste the list into the sort tool, one item per line.
  2. Under Order, choose Numbers instead of A→Z.
Plain A→ZNumbers
item1, item10, item2item1, item2, item10

The Numbers order reads the digits as whole numbers. Remove duplicates offers the same Numbers order. It removes repeats first and then sorts, so the output is unique and in numeric order.

Zero padding: the other fix

When you name files yourself, give every number the same width: 01, 02 and so on up to 10, or 001 to 100. A plain sort then gives the right order everywhere, including in programs with no natural sort. Padding also keeps the order when a list leaves your computer, because the file names carry the order themselves. Padding works because equal widths make a character-by-character comparison match the numeric order. The width must fit the largest number. A three-digit scheme holds up to 999, so a file named 1000 would sort between 100 and 101 and break the order. When a list grows past its width, pad every number to the wider width.

Use it for new files, and rename existing ones in a batch by adding the zeros.

Where a plain sort shows up

Numbered names appear everywhere: photos exported as IMG_1 to IMG_120, chapters named Chapter 1 to Chapter 30, track lists, invoice numbers and software versions. Any of them can look wrong after a plain sort, because the first digit decides the position. Chapter 10 lands next to Chapter 1, and Chapter 2 lands near the end.

Labels with letters work the same way. Part 2a, Part 2b and Part 10 sort in that order under natural sorting, because the digits are compared as numbers first and the letters only break ties within the same number. A plain sort puts Part 10 before Part 2a.

Dates need care. Day-first dates such as 4/11/2026 and 12/01/2026 sort by the day, not the date, so 4/11 comes before 12/01 even though 12 January is earlier in the year. Dates written year first, such as 2026-11-04, sort correctly as plain text, so use that format when a list must sort by date.

Check the result after sorting

Check the boundaries, where a plain sort most often goes wrong: the change from 9 to 10, and from 99 to 100. In a Numbers sort those points sit in order. If a list mixes padded and unpadded numbers, look at those points first, because the mix shows there.

Choose the order on purpose

Use A to Z for words, names and titles, where the letters carry the meaning. Use Numbers for lists whose items start with a count, a page, a chapter or a version. Use length when the shortest items should come first. Shuffle is for picking a random order, and it gives a different result each time you press it.

Edge cases

  • Mixed lines. Natural sorting compares whole lines from the left. In a list that starts with a name, the name decides the order first, so "Bob 1" comes after "Ada 10" even though 1 is smaller than 10.
  • Mixed widths. Padded and unpadded numbers in one list can compare as equal in a natural sort, so their order then depends on the input. Pick one width for each list.
  • Decimals. A natural sort reads 1.9 as 1, then 9, and 1.10 as 1, then 10, so 1.9 comes first. That suits version numbers, where 1.10 is the later release. For decimal values such as prices, 1.10 is less than 1.9, so the natural order is wrong. Pad the decimals to the same number of places: 1.10 and 1.90.
  • Case. The sort treats Apple and apple as the same by default, so they sort together in the order they were pasted. Case-sensitive changes that.
Cover of How to Remain Valuable When Intelligence Becomes CheapHow to Remain Valuable When Intelligence Becomes Cheap

224 pages on the human, economic and strategic advantages that stay scarce when AI can do most cognitive work.

$3.84 Get it on Gumroad →