Skip to content
RightYantra
Text & developer

Sort Text Lines

Put any list in order: alphabetically, in natural order so file2 comes before file10, by the number in each line, or by length. Reverse it or shuffle it fairly, drop duplicates and blank lines along the way, and get your line endings back exactly as they were.

How to use it· 4 steps
  1. Paste your list, one item per line, or open a text file.
  2. Choose what to sort by and in which direction.
  3. Set the options: ignore case, ignore surrounding spaces, unique lines only, remove empty lines.
  4. Copy the sorted list or download it as a .txt file.
Sort Text LinesRuns on your device

Step by step

How to use the sort text lines

  1. 1Paste your list, one item per line, or open a text file.
  2. 2Choose what to sort by and in which direction.
  3. 3Set the options: ignore case, ignore surrounding spaces, unique lines only, remove empty lines.
  4. 4Copy the sorted list or download it as a .txt file.

Three different orders that all look like “sorted”

Take file1, file2 and file10. Alphabetical order compares character by character, sees that 1 comes before 2, and puts file10 second: file1, file10, file2. That is correct alphabetically and wrong to everyone who has ever numbered files. Natural order reads the digits as numbers and gives file1, file2, file10 — the order a file manager shows.

Numeric order goes further and sorts by the value of the first number in each line, whatever the words around it, so “10 apples”, “9 pears” and “£1,299 laptop” come out as 9, 10, 1,299. Use it for lists of prices, scores, measurements or anything that leads with a quantity. Lines with no number in them go to the end, whichever direction you sort.

As a rule of thumb: alphabetical for words and names, natural for file names, product codes and chapter lists, numeric when the number is the point.

How alphabetical order is decided

Alphabetical order follows the language rules of your browser, through the same collation it uses for sorting everywhere else. That is why “éclair” sorts next to “eclair” rather than after “zebra”, which is where a plain character-code sort puts every accented letter. Languages with their own rules get them: with the browser set to Swedish, å, ä and ö come after z, as a Swedish reader expects.

Ignore case, on by default, treats Apple and apple as equal and leaves equal lines in the order you gave them. With it off, case still only breaks ties: the two stay side by side, rather than every capitalised word jumping ahead of every lowercase one. That strict capitals-first ordering is what the Unix sort command produces in its C locale; it is a character-code order, not a dictionary one.

Ignore leading and trailing spaces, also on by default, stops an accidental space from sending a line to the top of the list. The lines themselves are left exactly as they were.

Natural and numeric, in detail

Natural sorting splits each line into runs of digits and runs of everything else and compares them run by run — numbers by value, text alphabetically and without regard to case. This site uses its own implementation rather than the browser's built-in numeric collation, because Firefox's version was measured giving inconsistent answers for names like jesc101 and jesc1an, and an inconsistent comparison makes a sort return a scrambled list. This one behaves identically in every browser.

Numeric sorting reads the first number in a line the way a person would: “Chapter 10” is 10, “-4 °C” is minus four, “$1,299.50” is 1,299.5. A full stop is the decimal point and a comma followed by three digits is a thousands separator, so “3,5” is read as 3 — European decimal commas are not understood. A hyphen inside a word, as in item-5, is not taken for a minus sign.

Stable sorting, length, reverse and shuffle

Every sort here is stable: lines that compare as equal keep their original order relative to one another, in both directions. That is what makes sorting by length predictable, and what stops Apple and apple trading places at random in a case-insensitive sort.

Length sorting counts characters as you see them, so an emoji counts once. Reverse flips the list end to end without sorting it, which is handy for logs and anything written newest-last.

Shuffle uses the Fisher–Yates algorithm with your browser's cryptographic random number generator, so every possible order is equally likely. The common shortcut of sorting with a random comparison function is not fair: it produces a biased order, and the bias even differs from browser to browser. Use shuffle for a random running order of speakers, questions or test cases, and press Shuffle again for another.

Remove empty lines is on by default, because blank lines sort ahead of everything else and would otherwise collect at the top of an A–Z list. Unique lines only drops repeats after sorting, using the same case and space rules; to remove duplicates without changing the order, use the remove duplicate lines tool instead.

Answers

Frequently asked questions

Why does 10 come before 2 when I sort?

Because alphabetical sorting compares characters, and the character 1 comes before 2. Choose Natural or Numeric to compare numbers by their value.

How do I sort by the number at the start of each line?

Choose Numeric. It reads the first number in each line — decimals, negatives and thousands separators included — and puts lines without a number at the end.

Is the sort stable?

Yes. Lines that are equal for the chosen order stay in the order they were in, whether you sort ascending or descending.

Is the shuffle really random?

Yes. It uses the Fisher–Yates shuffle with cryptographic randomness, so every ordering of the lines is equally likely.

Can I sort by a column or the second word?

No — this tool sorts whole lines. For sorting CSV or tab-separated data by a column, a spreadsheet is the right tool.

Does it handle accented letters and other languages?

Yes. Alphabetical order uses your browser's language rules, so accented letters sort with their base letters and languages with their own alphabetical order get it.

Are my lines uploaded?

No. Sorting happens in your browser.