Sort Lines
Sort lines alphabetically, numerically, by length, or shuffle them randomly.
Sort Lines tool
What this tool does
Sorts a list of lines five different ways: alphabetically, in natural order, by the number each line starts with, by line length, or shuffled into a random order. Each mode can be reversed, made case-sensitive, told to ignore indentation, and told to throw away blank lines first.
Natural order is the one worth the visit. Plain alphabetical sorting compares text character
by character, so file10 lands before file2 — because
1 precedes 2. Natural order reads runs of digits as numbers and
puts them where a human expects.
Common uses
- Alphabetising a list of imports, environment variables, dependencies or CSS custom properties so the next diff is readable.
-
Ordering filenames or version strings correctly —
v1.9.0beforev1.10.0, which alphabetical sorting gets backwards. - Ranking log lines by a leading duration or count with numeric mode.
- Sorting by length to find the outlier — the one 300-character row in a file of 40-character rows is almost always the malformed one.
- Shuffling a list to pick a random order for a rota, a test fixture or a draw.
A short example
Alphabetical order gives:
file1.txt
file10.txt
file2.txt
file20.txt
file3.txt Natural order gives what you meant:
file1.txt
file2.txt
file3.txt
file10.txt
file20.txt How each mode compares
Alphabetical compares by Unicode code unit, lowercased unless "case
sensitive" is on. With case sensitivity enabled, every uppercase ASCII letter sorts before
every lowercase one, so Zebra precedes apple — that is how
sort behaves in the C locale, and it surprises people every time.
Natural walks both strings as alternating runs of digits and non-digits.
Digit runs compare by value, and because they are compared by length-then-text rather than
by parsing to a float, IDs longer than 15 digits still order correctly — a real problem for
any implementation that reaches for parseInt. Zero-padded forms are kept
deterministic: img001 groups just ahead of img1.
Numeric reads a number from the start of each line, understanding a leading
sign, a decimal point and thousands separators, and sends lines with no number to the bottom
in their original order. Length counts UTF-16 units. Shuffle
uses a Fisher-Yates pass, which is uniform — unlike the sort(() => Math.random() -
0.5) trick, which is measurably biased.
Worth knowing
Every sort here is stable, so lines that compare equal keep their original relative order. That lets you chain passes: sort by one key, then by another, and the first ordering survives inside each group of the second. "Use result as input" makes that a single click.
Sorting is done on the line's text, not on a locale collation. ä therefore sorts
after z rather than beside a, which is right for identifiers and
wrong for a German phone book. For human-language lists in a non-English locale, your
spreadsheet's locale-aware sort is the better tool.
Frequently asked questions
Why does the sort command give a different order than this page?
Locale. Under a UTF-8 locale, GNU sort uses locale collation, which ignores punctuation and case at the first comparison level, so a-b, ab and Ab interleave in ways byte order never produces. LC_ALL=C sort switches to plain byte comparison and matches the alphabetical mode here with case sensitive enabled. Pinning that variable is what stops a generated file from reshuffling itself on a colleague’s machine.
Can I sort a JSON, YAML or CSV file this way?
Only if it is a flat list. Sorting the lines of a JSON document moves closing braces and commas away from the structures they close, and the result will not parse — nested data is not a set of interchangeable rows. Sort the keys with the JSON formatter instead. A CSV is safe only once the header row is removed, or it will be sorted into the middle of the data.
Is it safe to alphabetise a .env file or a block of imports?
Some files are order-dependent. Dotenv-style expansion resolves top to bottom, so BASE_URL=${HOST}/api breaks as soon as it sorts above HOST. In CSS, @import must stay ahead of every ordinary rule, and the last declaration of equal specificity is the one that wins. Language import blocks are usually safe to reorder — that is what isort and ESLint’s sort-imports do — but configuration files often are not.