Sort Lines Alphabetically
Sort lines alphabetically A–Z or Z–A, with natural number sorting, case sensitivity, blank-line removal and reverse order. Runs in your browser.
Worked examples
- A simple alphabetical list
Case-insensitive ascending sort: Apple and apple sort together, and because the sort is stable they keep their original relative order.
- Natural sort for file names
With natural sort on, the number inside each name is compared as a number, so file2 comes before file10. A plain sort would put file10 first.
- Code-point order, blank lines removed, descending
Code-point order puts every uppercase letter before every lowercase one, which is how many tools and programming languages sort by default; descending reverses the whole order.
What this tool does
This tool sorts a block of text line by line: ascending or descending, with or without case sensitivity, with optional natural number ordering and blank-line removal, or simply reversing the existing order. It runs in your browser and shows how many lines it kept and how many blanks it dropped.
When you need it
- Alphabetizing a list of names, imports, dependencies, hostnames or keywords.
- Ordering file names or version-like strings that contain numbers, where
file2should come beforefile10. - Normalizing a config or allow-list so diffs show only real changes.
- Reversing a log or list without sorting it.
The options
Order. Ascending is A to Z, descending is Z to A, and reverse flips the lines exactly as they are without comparing anything. Ties keep their original relative order in both directions, because the sort is stable; descending is the same comparison with the arguments swapped, not an ascending sort that is then reversed.
Case sensitive. Off by default, so Apple and apple are treated as equal and sit next to each other in input order. With it on, they are distinct.
Natural sort. Compares runs of digits by number, so file1, file2, file10, file20 come out in that order. A plain text sort compares 1 and 2 as characters and puts file10 before file2, because 1 is smaller than 2. Only digits 0 to 9 are recognized as numbers. Leading zeros do not change the value, so x02 sorts with x2.
Remove blank lines. Drops empty and whitespace-only lines before sorting. Without it, blank lines sort to the top in ascending order because an empty string is smaller than any other.
Locale versus code-point comparison
This is the choice that explains most "why is my sort in a weird order" questions.
Locale mode uses the English collation rules from Intl.Collator, the same dictionary-style ordering as a word processor. Accents sort next to their base letters, so é comes between e and f, and when case matters the lowercase form sorts before the uppercase one (a, A, b, B). The tool pins the locale to English so output is identical on every machine instead of varying with the visitor's browser language. If your language orders letters differently (Swedish puts ä after z, for example), this mode will not match it.
Code-point mode compares the Unicode number of each character, which is what LC_ALL=C sort (byte order, identical to code-point order for UTF-8 text), Python's default string comparison and database binary collations do. Every uppercase letter then comes before every lowercase letter (B before a), digits come before letters, and accented letters come after z. Use it when the result must match what a program would produce, such as sorting keys that will be binary-searched. The tool compares true code points, not UTF-16 units, so characters outside the Basic Multilingual Plane, such as emoji, sort after characters like U+FF21 (fullwidth A), which a naive JavaScript < comparison gets wrong.
With case sensitivity off, code-point mode compares lowercased text, so B and a sort as a, B.
Line breaks and trailing newlines
Input is split on \n, \r\n or \r, and the output uses \n. If your text ends with a newline, the output does too, and the empty "line" after it is not counted or sorted. Leading and trailing spaces inside a line are not trimmed, and they affect order: a line starting with a space sorts before one starting with a letter.
Limits
Input is capped at 200,000 characters. Sorting is done on whole lines, not columns; to sort by a later field, move that field to the start of each line first. To remove repeated lines after sorting, use the duplicate-line remover.
Frequently asked questions
- What is the difference between locale and code-point comparison?
- Locale comparison uses dictionary rules (English collation here, so results match on every machine): accents sort next to their base letter, so é falls between e and f, and case is a tiebreaker. Code-point comparison orders strictly by Unicode number, so all capitals come before all lowercase letters and é sorts after z. Use locale for human-readable lists and code-point for matching what a program or `LC_ALL=C sort` would do.
- What is natural sort?
- It compares digit runs by their numeric value instead of character by character, so item2 sorts before item10. Without it, a text sort compares 1 and 2 as characters and puts item10 before item2.
- Where do blank lines go?
- In ascending order they sort to the top, because an empty line is shorter than any other. Turn on remove blank lines to drop empty and whitespace-only lines before sorting; the count of removed lines is shown.
- Is the sort stable, and does a case-insensitive sort keep the original order of ties?
- Yes. The browser's sort is stable, so lines that compare as equal, such as Apple and apple in a case-insensitive sort, keep the relative order they had in your input. In descending order the tie order is also preserved rather than flipped.