How to Sort a List Alphabetically
Sorting a list alphabetically sounds like the least interesting task in computing. Then you sort your files and find file10 sitting between file1 and file2, or all the capitalised names clumped above the lowercase ones, and it stops being obvious. The computer is not broken — it is doing exactly what it was told. This guide explains what “alphabetical” actually means to a machine, and how to get the order you meant.
How computers really compare text
A computer does not know the alphabet. It knows numbers.
Every character has a numeric code — A is 65, B is 66, a is 97, 0 is 48. To sort, the computer compares these codes, character by character, left to right. The first position where two strings differ decides the order. If one string runs out first, it sorts earlier.
For plain lowercase English words this produces exactly what you expect. Everything else in this article is a consequence of it producing something you did not expect.
Why 10 comes before 2
The single most common sorting complaint. You sort a list and get:
item1, item10, item11, item2, item20, item3
This looks like a bug. It is not. Remember: the comparison is character by character, and it does not know that 10 is a number. It compares item1 against item2 one character at a time, reaches the fourth character, and finds 1 versus 2. Since 1 sorts before 2, everything starting with item1 — including item10 and item19 — comes before anything starting with item2.
The computer is treating 10 as the characters one then zero, not as the value ten. Which is precisely what alphabetical sorting means.
There are two ways to get the order you actually wanted:
- Natural sort. A smarter algorithm that recognises runs of digits and compares them as numbers. This gives
item1, item2, item3, item10, item20. It is what file managers use, and it is what humans mean by “in order.” - Zero-pad your names. Rename to
item01, item02, item10. Now plain alphabetical sorting gives the right answer, because all the numbers are the same width. This is the trick to use when you control the naming — and it is why professionals name filesscan001.pdfrather thanscan1.pdf.
The zero-padding habit is worth adopting permanently. It makes every tool you ever use — sorting, merging, batch processing — behave correctly with no special handling.
Why capitals sort first
Sort apple, Banana, cherry and a naive sort returns Banana, apple, cherry. The capital jumped to the front.
The reason is in the character codes: all uppercase letters (65–90) come before all lowercase letters (97–122). So Z (90) sorts before a (97). Every capitalised word outranks every lowercase one, regardless of the actual letters.
This is case-sensitive sorting, and it is almost never what you want for a list of names or words. The fix is a case-insensitive sort, which compares as if everything were lowercase and gives you apple, Banana, cherry.
Keep case-sensitive sorting for code, identifiers and anything where ABC and abc are genuinely different values.
Accented and non-English letters
Accented characters have high numeric codes, so a naive sort exiles them to the end of the list — after z. That gives you apple, zebra, éclair, which no human would accept.
Proper sorting uses a locale-aware collation, which knows that é belongs with e. And the correct order genuinely differs by language, which is a lovely illustration of why this is harder than it looks:
- In Swedish,
äandöcome afterz— they are distinct letters at the end of the alphabet. - In German,
äis sorted as if it werea.
Same character, different correct answer, depending on who is reading. There is no universal alphabetical order.
Other sorting orders worth knowing
- Reverse alphabetical (Z–A) — for finding the end of a list quickly.
- By length — surprisingly useful for keyword lists, since short keywords tend to be high-competition head terms and long ones are long-tail.
- Numerically — treating each line as a number rather than text.
- Randomly (shuffle) — for drawing names or randomising an order.
- Reverse the current order — no sorting, just flipping what you have.
Stable sorting
A worthwhile piece of jargon. A sort is stable if items that compare equal keep their original relative order. If two lines are identical, a stable sort leaves the one that was first still first.
It matters when you sort by one field and then another: sorting by surname, then by department, a stable sort leaves each department's people still in surname order. An unstable sort scrambles them, and you have to start over.
Clean before you sort
Sorting exposes messy data rather than fixing it. Trailing whitespace, in particular, quietly ruins the result — a leading space has a very low character code, so a line beginning with a space jumps to the top of the list for no visible reason.
So: trim the whitespace, remove the empty lines, and deduplicate before sorting. The output will make far more sense.
Frequently asked questions
Why does my sorted list put numbers in the wrong order? Because it is sorting them as text. Use a natural sort, or zero-pad the numbers.
Why are capitalised words at the top? Case-sensitive sorting. Switch to case-insensitive.
Why is a blank-looking line at the very top? It probably starts with a space. Trim whitespace first.
Can I sort by the second word in each line? Not with a simple line sort — that needs column-aware sorting, or rearranging the text first.
Sort your list now
Use our Text Sorter to sort any list alphabetically, in reverse, or by length — in your browser, with nothing uploaded. Clean the list first with Remove Duplicate Lines, and compare two sorted lists with the Text Diff Checker.