Skip to content

Sort Lines

Result

0 lines

Estimates for educational purposes — not financial, medical, or legal advice. See terms.

Sorting a list of lines sounds trivial — every text editor has a “Sort Lines” command. But the default JavaScript sort surprises people: it compares strings character-by-character using Unicode code points, which puts file10 before file2 because '1' (codepoint 49) comes before '2' (codepoint 50). For lists that mix text with numbers, that’s almost never what you want.

This tool gives you four sort modes and four directions, plus the cleanup options most people end up needing immediately after a sort: deduplicate, remove blank lines, trim whitespace, ignore case.

The four sort modes

Alphabetical is the standard lexicographic sort. Fast, predictable, but treats numbers as text — file10 lands before file2. Use it for plain text where the numeric quirk doesn’t matter.

Natural recognizes runs of digits inside strings and compares them as numbers. file2 correctly sorts before file10. Use it for filenames, version strings (v1.10 after v1.9), chapter numbers, or anything mixing text and numbers.

Numeric parses each line as a single number. Use it when every line is just a number — prices, durations, IDs. Lines that don’t parse as numbers float to the bottom in alphabetical order.

Length sorts by character count, with alphabetical as a tiebreaker for equal-length lines. Useful for finding the longest or shortest entries quickly, or when checking which lines fit a width limit.

The four directions

Ascending is A→Z (or 0→9, short→long). Descending is Z→A — same sort, reversed order.

Reverse flips the input order without sorting at all. Useful when your input is already in a meaningful order (chronological log lines, ordered build steps) and you want to see it inverted.

Shuffle randomizes the order. Useful for fair drawings, randomized teaching prompts, or quick test ordering.

Example: cleaning a list of email addresses

You paste a list of 200 email addresses scraped from various sources. Some are duplicated (the same address with different capitalization), some have leading or trailing whitespace, and you want the unique addresses in alphabetical order.

Turn on trim whitespace, case-insensitive, and remove duplicates. Pick alphabetical, ascending. The result is a clean, sorted, deduplicated list. The counts panel tells you how many duplicates and blank lines were removed, so you can sanity-check the cleanup.

Example: ordering version-tagged files

A directory listing produces release-v1.2.txt, release-v1.10.txt, release-v1.9.txt. Plain alphabetical sort orders them v1.10, v1.2, v1.9 — wrong. Switch to natural sort and they come out v1.2, v1.9, v1.10 — correct.

Common mistakes

Picking numeric sort for text-with-numbers. Numeric sort tries to parse the whole line as one number. If your lines are file2 and file10, both parse as NaN and fall to the bottom in alphabetical order — meaning numeric sort would just give you file10 then file2, the same as alphabetical. Use natural sort for anything where the numbers are embedded in text.

Dedupe with case-sensitive on, expecting case-insensitive behavior. If Apple and APPLE are duplicates to you, turn off case-sensitive — otherwise dedup treats them as distinct strings. The case-insensitive toggle affects both sorting and dedup.

Forgetting that a trailing newline counts as a blank line. A file ending in \n has an empty line at the end. The “remove blank lines” option will drop it; the count will reflect it. If your input line count looks one higher than expected, the trailing newline is usually why.

What this tool does not do

It does not sort by a column or field within each line — for CSV-style data, sort it in a spreadsheet or with sort -t, -k2 on the command line. It does not preserve groupings or sort within groups. It treats every line as an independent string. For pattern-based edits on the whole input (regex, plain, case-insensitive), the find and replace tool is the matching sibling.

Frequently asked questions

What's the difference between alphabetical and natural sort?

Alphabetical (lexicographic) compares strings character-by-character using Unicode code points. That puts file10 before file2 because the character '1' comes before '2'. Natural sort recognizes runs of digits inside strings and compares them as numbers, so file2 sorts before file10. Use natural for anything that mixes text and numbers — filenames, version strings, chapter numbers.

When should I use numeric sort instead of natural?

Numeric sort parses each entire line as a single number. Use it when every line is a pure number ('100', '9', '42') and you want them ordered as numbers. Natural sort handles mixed lines like 'file100' and 'file9' where the digits are embedded in text. Numeric sort puts non-numeric lines after the numeric ones.

Is the deduplication stable? Which copy is kept?

Dedupe runs before sorting, in input order, keeping the first occurrence of each line. So if your input is 'foo, bar, foo, baz', the duplicate 'foo' on the third line is removed and the order becomes 'foo, bar, baz' before sorting. After dedupe the result is sorted as usual.

How does case-insensitive sorting affect dedup?

When 'case-insensitive' is on, both sorting and dedup ignore case. So 'Apple' and 'APPLE' are treated as the same string for both — they sort together and dedup collapses them to one. The kept copy is the first occurrence in input order, with its original casing preserved.

What's the difference between 'reverse' and 'descending'?

Descending sorts the list, then reverses the sort order — so it's a true Z-A sort. Reverse just flips the input order without sorting at all, useful when you want to invert a manually-ordered list (like a chronological log read newest-first). Both are also useful with shuffle, which randomly reorders without sorting.