How to Sort Words Alphabetically (Case, Digits and Accents)
Flashcards get shuffled, a keyword list needs alphabetical order, and near-duplicates are easier to spot when similar spellings sit together. Sorting is simple, but it follows fixed rules that surprise people.
When sorting words helps
Indexes, glossaries and vocabulary lists need alphabetical order, and sorting several dozen entries by hand is where omissions happen. A scrambled word list also benefits from being restored to dictionary order so it can be checked against a textbook.
The other use is spotting near-duplicates. Once sorted, similar spellings sit next to each other, so variants such as color and colour or organise and organize become obvious.
- Alphabetising keywords and glossary entries
- Restoring a shuffled vocabulary list
- Finding spelling variants of the same word
- Preparing alphabetically ordered index entries
The rules behind the order
Words are split on whitespace and compared as strings, character by character by code value. Every uppercase letter sorts before every lowercase letter, so Apple lands before apple and all capitalised words cluster at the top.
Digits compare as characters rather than values, so 10 comes before 9 because the character 1 is smaller than 9. Accented letters have higher codes, so é sorts after z, and Chinese characters sort by code point with no relation to pinyin. The result is joined into one line with single spaces, and duplicates are kept.
- Case-sensitive: capitals all come before lowercase
- Digits compare as characters, so 10 precedes 9
- é sorts after z rather than next to e
- Duplicates are kept, once per occurrence
- Output is a single space-joined line
What it cannot do, and what to use instead
It is not a natural sort: 2 does not come before 10, and é is not treated as e. Case-insensitive or locale-aware ordering is out of reach, and so are Chinese pinyin order and Japanese kana order — you get code-point order instead.
To keep lines intact, use the line sorter, which sorts row by row. To ignore case, use the case-insensitive line sort. On the command line, sort follows the locale by default, sort -f ignores case, and sort -V handles version numbers.
- No natural sort — digits compare as characters
- No locale collation (pinyin, kana unsupported)
- Keep lines: use the line sorter
- Ignore case: use the case-insensitive line sort
FAQ
Why do capitalised words come before lowercase ones?
Because comparison uses character codes, and uppercase letters have lower values than lowercase ones, so Apple sorts before apple. Lowercase the whole text before sorting, or use the case-insensitive line sorter, to remove that effect.
Why does 10 come before 9?
Digits are compared character by character, so the first character decides: 1 is smaller than 9 and 10 lands before 9. For numeric order, pad the numbers to equal length first, as in 09 and 10, or use a tool with numeric sorting.
Where did my line breaks go?
Sorting splits on spaces and line breaks, then joins everything with single spaces, so multiple lines collapse into one. Use the line sorter instead when the line structure has to survive.
Related tool:Sort Words