Skip to the tool
DevToolBench

Sort Lines

Sort lines alphabetically, naturally, numerically or by length.

0 lines
0 lines

Everything runs in your browser. Nothing you type is sent to a server.

Found a bug in this tool? Report it.

Share this tool

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

  1. Paste the lines.
  2. Pick the order, then reverse it if you want the other end first.
  3. 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.

Frequently asked questions

What is natural sort, and when do I want it?

Natural sort reads runs of digits as numbers instead of characters, so file2 lands before file10 rather than after file1. Choose it for anything numbered by humans — screenshots, chapters, version tags, invoice references. Choose plain alphabetical when the strings are identifiers that only happen to contain digits and you want a strictly predictable order.

Why does a capital letter no longer come first?

Because this page sorts the way a reader expects rather than the way a byte comparison works. Comparing raw characters puts every capital ahead of every lowercase letter, so Zebra precedes apple. The collator used here treats case as a tiebreaker within a letter, which is what a dictionary does.

Is the sort stable?

Yes. Lines that compare as equal keep the order they arrived in, which matters most when sorting by length or by leading number, where ties are common. Reversing negates the comparison rather than flipping the finished list, so ties stay in their original order in both directions.

What happens to lines with no number in numeric mode?

They sink to the bottom, keeping their relative order, because there is nothing to compare. Reversing floats them to the top, since reverse inverts the entire comparison. If they are noise, tick the option to drop empty lines and remove the rest before sorting.

Why does my result differ from sort in the terminal?

Locale, almost certainly. Running sort with LC_ALL=C compares bytes, which puts all capitals first and scatters accented letters after z. Without that setting, sort uses your system locale and gets closer to this page. Neither is broken; they answer different questions.

Related tools

Updated