Text Tools
How to Sort Lines of Text Alphabetically or Numerically
Sort lines of text alphabetically, numerically or in reverse, with case and locale options, and avoid the classic lexicographic sorting mistakes.
Try the toolSort Text Lines →The problem: sorting is not as obvious as it looks
Alphabetizing a list, ordering version numbers, arranging log lines by timestamp: sorting text is a daily task, and the "obvious" default almost always gets numbers wrong. If you have ever seen 1, 10, 2, 3 come out of a sort, you have met the classic lexicographic trap.
How text sorting works
By default, most languages compare strings character by character using their code-point values. That means Z (code 90) sorts before a (code 97), and 10 sorts before 2 because 1 comes before 2. Watch:
['banana', 'Apple', 'cherry'].sort();
// ["Apple", "banana", "cherry"] — uppercase wins
['10', '2', '1'].sort();
// ["1", "10", "2"] — lexicographic, not numeric
To sort the way humans expect, you pass a comparator. For numbers, subtract:
['10', '2', '1'].sort((a, b) => a - b);
// ["1", "2", "10"]
For case-insensitive, locale-aware alphabetical order, use localeCompare:
['banana', 'Apple', 'cherry'].sort((a, b) =>
a.localeCompare(b, undefined, { sensitivity: 'base' })
);
// ["Apple", "banana", "cherry"] — 'a' and 'A' now rank equally
And for natural sort, where file2 comes before file10, turn on the numeric option:
['file10', 'file2', 'file1'].sort((a, b) =>
a.localeCompare(b, undefined, { numeric: true })
);
// ["file1", "file2", "file10"]
A practical walkthrough
Say you have a list of 200 country names to alphabetize for a dropdown, and a few start with accented letters like Åland and Côte d'Ivoire. A naive sort pushes the accented entries to the bottom. Paste the list into the Sort Text Lines tool, choose alphabetical and case-insensitive, and locale-aware comparison puts them where a reader expects. Flip to reverse order and you have Z to A without editing anything by hand.
Common pitfalls
- Numbers as strings. The single most common mistake.
100sorts before99lexicographically. Use numeric comparison for anything that is really a number. - Mixed content. Version strings like
1.9.0and1.10.0need per-segment numeric comparison; plain sorting puts1.10.0first. Natural sort handles most of these. - Case surprises. A case-sensitive sort groups all uppercase before all lowercase, which looks broken to non-programmers. Case-insensitive is usually what people want for display.
- Locale differences. In Swedish,
äsorts afterz; in German it sorts neara. There is no single "correct" order; it depends on the language. - Stability. Equal lines should keep their relative order. Modern JavaScript sort is stable, but older comparison hacks may not be.
Related tools
Sorting and deduping go hand in hand: the Remove Duplicate Lines tool strips repeats before or after you sort. If your lines are wrapped across multiple physical lines, Remove Line Breaks can flatten them first so each item is a single sortable line.
Conclusion
The gap between a naive sort and a correct one is almost always about numbers and case. Once you know to reach for numeric comparison and locale-aware, case-insensitive ordering, sorting stops surprising you. The Sort Text Lines tool offers alphabetical, numeric and reverse ordering with case options, all running privately in your browser.