How to Sort Lines Alphabetically (and Four Other Sorts)
Alphabetical, numeric, by length, reverse, and random. Five sort orders, each useful in a different context. The keystrokes to get there in the browser, in Excel, and from the command line.
The five orders
Five sort orders cover ~95% of practical sort tasks. Each one is a different button click in the browser tool:
- Alphabetical (A → Z). The default. Compares character by character using the locale's collation rules.
- Reverse alphabetical (Z → A). Same as above, reversed.
- Numeric. Treats lines as numbers. Critical for IDs, prices, counts. Without numeric mode, "10" sorts before "2".
- By length (shortest first or longest first). Counts characters per line and sorts by that. Useful for finding the longest URL, the shortest password.
- Random. The Fisher-Yates shuffle. Same algorithm used by the dedicated Shuffle Lines tool.
Browser — the 5-second path
Open the Sort Lines tool. Paste your list. Click the order you want. Copy the result. Done.
The non-obvious option: case-insensitive vs case-sensitive. Browser tools usually default to case-insensitive because it matches human expectation. ASCII-strict sorting puts all uppercase letters before all lowercase, which is rarely what you want for human-readable lists.
Excel / Google Sheets
For lists already in a spreadsheet:
- Select the column.
- Data → Sort → A to Z (or Z to A, or custom).
For numeric sort to work, the cells must contain actual numbers, not text representations. Excel and Sheets both auto-detect this — but if you've imported numbers as text, change the cell format first.
For multi-column sort (sort by column A, break ties with column B), use the Custom Sort dialog and add additional sort levels.
Command line — sort
The Unix sort command is the production-strength tool. The most useful flags:
| Flag | Effect |
|---|---|
sort file | Alphabetical, locale-aware |
sort -r file | Reverse |
sort -n file | Numeric (treats lines as numbers) |
sort -f file | Case-insensitive |
sort -u file | Sort + dedup |
sort -R file | Random (shuffle) |
sort -k 2 file | Sort by the second whitespace-separated field |
sort -t, -k 2 file | Sort by the second comma-separated field (CSV) |
For files larger than RAM (more than a few GB on a typical laptop), sort automatically uses an external merge sort and works correctly without any flag adjustments.
The traps
Trap 1: alphabetical-vs-numeric on lists of numbers. The list [1, 2, 10, 100, 11] alphabetically becomes [1, 10, 100, 11, 2]. Always pick numeric sort for numbers.
Trap 2: invisible whitespace. A list pasted from elsewhere can have trailing spaces, leading tabs, or non-breaking spaces. These affect sort order. Trim before sorting if cleanliness matters. The Sort Lines tool offers a "trim whitespace" toggle.
Trap 3: locale-aware sorting on non-English text. German ä sorts after z in some locales, between a and b in others, and as equivalent to ae in still others. Set the locale explicitly (or use LC_ALL=C for code-point order) when reproducibility matters.
Trap 4: stable vs unstable sort. A stable sort preserves the original order of items that compare equal. An unstable sort doesn't. For most browser sort tools and Unix sort, you get stable sort. If you're chaining sorts (sort by primary key, then by secondary key), stability matters — use it.
Practical examples
Sort a list of URLs to find the longest. Sort by length descending. The first line is the longest URL. Useful for spotting tracking-parameter pollution.
Sort a CSV column of prices. Numeric sort. Prefix-strip the dollar sign first if needed (find/replace $ → empty).
Sort a list of names "Last, First" alphabetically by last name. Standard alphabetical works because the comma comes before the first name. The list sorts by everything before the comma first.
Sort a list of URLs by domain. Hard with line-only tools. Drop to awk or Python: parse the URL, extract the domain, sort by that.
For the deeper reference on list operations including dedup, shuffle, and reformat, see List Operations: The Complete Guide. For shuffling specifically, see How to Shuffle Lines (Fisher-Yates).
Frequently asked questions
Why does '10' sort before '2' alphabetically?
Because alphabetical sort compares character-by-character. '1' is less than '2', so '10...' sorts before '2...'. For numeric sort, use a numeric-aware tool that treats '10' as 10.
How do I sort case-insensitively?
Most modern sort tools have a case-insensitive toggle. The TextKit Sort Lines tool defaults to case-insensitive. In Excel, use the Custom Sort dialog and check 'Match case' off. In bash, use sort -f or LC_ALL=C.UTF-8 sort.
What's the most efficient way to sort millions of lines?
For 1M+ lines, command-line sort on Linux/macOS is the fastest. It uses an external merge sort that handles files larger than RAM. Browser tools start to slow down past ~1M lines.
Why does my sort split apostrophes weird?
Some locales sort O'Brien by collation rules where the apostrophe ranks differently from how you'd expect. Set LC_ALL=C in shell sort, or pick 'ASCII order' in browser tools, to get plain code-point ordering.
Can I sort by a specific column in a CSV?
Yes — but not with a line-by-line sort tool. Use Excel, Google Sheets, or shell sort -t, -k 2 (sort by the second comma-separated field). Single-line tools sort by the entire line.
Keep reading
Written by the TextKit team. We build the tools we write about — try the Sort Lines tool used in this post.