What this tool does
This tool will sort lines from any list four ways: alphabetically by the rules of English, in natural
order so that file2 comes before file10, by the number at the start of each line, or by how long
each line is. Every order can be reversed, case can be ignored, and empty lines can be dropped before
sorting rather than piling up at the top.
Ties never shuffle. Lines that compare as equal come out in the order they went in, which is what makes sorting by length or by leading number usable on real data.
How to use it
- Paste the lines.
- Pick the order, then reverse it if you want the other end first.
- Copy or download the result.
Why "alphabetical" surprises people
Ask a program to sort text and, by default, it compares character codes. In that scheme every capital letter is "smaller" than every lowercase letter, because that is how ASCII was laid out in 1963. The result reads as nonsense:
Zebra
apple
banana
That is not a bug, it is a byte comparison, and it is what sort gives you under LC_ALL=C, what
Array.sort() gives you in JavaScript without a comparator, and what most database ORDER BY
clauses give you under a binary collation.
Accents make it stranger. Letters outside plain ASCII sit at higher code points, so Ångström lands
after Zulu — and in a list of names, that looks broken to everyone who reads it.
This page uses the browser's collator with the site's en-GB locale, so apple comes before
Zebra, case decides only between otherwise identical words, and accented letters sort next to
their base letter. Locale genuinely matters here: in Swedish, Å is a separate letter that belongs
at the very end of the alphabet, and a Swedish collator is right to put it there. There is no
universal alphabetical order, only a correct one per language.
Natural sort, and where it bites
Natural sort treats a run of digits as one number. img2 before img10; v1.9.0 before v1.10.0;
Chapter 9 before Chapter 10. It is what every file manager does, and it is what people mean when
they say a list is "out of order".
Two things to know before you use it everywhere. Leading zeros stop mattering — 007, 7 and 7.0
compare as the same number, so their relative order comes down to stability rather than the digits
you typed. And it is a display order, not a storage one: a database index will not produce it, so
sorting a page of results naturally in the browser gives a different sequence from paginating them
in the server. If a list must be reproducible across systems, pad the numbers in the data
(file002) and plain alphabetical will do the job everywhere.
Sorting by length, and by the number in front
By length is the trick for finding the odd one out. Sort a column of postcodes, product codes or API keys by length and anything malformed jumps to one end of the list. It counts code points, so an emoji counts as one character rather than two.
Numeric reads the number at the start of each line and ignores the rest of it, which is exactly
the shape of the output that log files, du -h listings and pasted spreadsheet columns produce.
Lines with no leading number sink to the bottom in the order they arrived, so you can see them
rather than losing them.
Sorting to find duplicates? Remove duplicate lines does that directly and keeps the original order, which sorting destroys. Need the totals afterwards? The word counter has them.