Sort 10, 9 and 2
sorted(["10", "9", "2"])The result is ["10", "2", "9"]. Ten comes first.
Nothing is broken. Look closely at the list: those are not numbers. They are strings, pieces of text that happen to contain digits. And text is not sorted by size.
How text is compared
Text is put in order the way a dictionary orders words: one character at a time, from the left, and the first difference decides.
The small number under each character is its code: the number the computer actually compares.
Start. Which comes first, "10" or "9"? Text is compared one character at a time, starting from the left.
Compare the first character of each
Characters are compared by their code numbers. For digits, "0" is lowest and "9" is highest.
If they differ, that settles it
Whichever has the lower character comes first. Nothing after it matters.
If they are the same, move one place right and repeat
Just as "apple" and "apply" are only separated at the fifth letter.
If one runs out, the shorter comes first
"app" comes before "apple".
Example
Why 10 comes before 9
Compare "10" and "9". First characters: "1" against "9". "1" is lower. Decision made: "10" comes first.
The "0" is never looked at. The fact that one string is longer, and stands for a bigger number, plays no part.
It is the same reason "aardvark" comes before "b" in a dictionary despite being longer.
Where it bites
This is one of the most common small bugs in programming, because text that looks like numbers turns up everywhere.
- Numbers read from a file, a form or a web request. They arrive as text. Sort them without converting and you get 1, 10, 100, 2, 20.
- File names.
file10.txtis listed beforefile2.txt. - Version numbers.
"1.10"sorts before"1.9", though version 1.10 is newer. - JavaScript's default sort. It converts everything to text first, even actual numbers:
[10, 9, 2].sort(); // [10, 2, 9]
[10, 9, 2].sort((a, b) => a - b); // [2, 9, 10]That last one surprises almost everyone the first time. In JavaScript, always pass a comparison function when sorting numbers.
The fixes
Convert to numbers before sorting, or tell the sort to compare by numeric value:
sorted(["10", "9", "2"], key=int) # ["2", "9", "10"]For mixed text and numbers, such as file names, use a "natural" sort that treats runs of digits as numbers. Many languages and file managers offer one.
For versions, split on the dots and compare the parts as numbers:
sorted(["1.10", "1.9"], key=lambda v: [int(part) for part in v.split(".")])
# ["1.9", "1.10"]Or pad with zeros so that text order and number order agree: file002, file010. This is why dates are best written as 2026-03-09: year, then month, then day, each padded. Sorted as plain text, they come out in order of time.
Capital letters come first
The same comparison explains another surprise. Every capital letter has a lower code than every small letter, so "Zebra" sorts before "apple". Try it in the demo above.
To sort the way people expect, compare lower-cased versions:
sorted(["Zebra", "apple"], key=str.lower) # ["apple", "Zebra"]Key takeaways
- Text is compared character by character from the left. The first difference decides.
- "10" comes before "9" because "1" comes before "9". Length and value do not matter.
- Numbers that arrive as text must be converted before sorting.
- JavaScript's sort compares as text unless you give it a comparison function.
- Zero-padded numbers and year-month-day dates sort correctly even as text.