Home/Text tools/Sort lines

Text tool

Sort Lines

Seven ways to order a list of lines, including a natural sort that gets file2 before file10 right where plain alphabetical order gets it wrong.

Why file10 sorts before file2 in plain alphabetical order

A → Z sorting compares text one character at a time. Comparing "file10" and "file2", both strings agree on f-i-l-e, then the fifth character decides it: "1" versus "2". Since the character 1 sorts before 2, "file10" wins the comparison — the sort never looks far enough to notice that 10 is bigger than 2, because it stopped comparing after one digit.

The same six filenames, lexicographic order versus natural order
#Lexicographic (A→Z)Natural sort
1file1.txtfile1.txt
2file10.txtfile2.txt
3file2.txtfile9.txt
4file9.txtfile10.txt

Natural sort fixes this by splitting each line into chunks of letters and chunks of digits, then comparing letter-chunks as text and digit-chunks as numbers. "file" vs "file" ties, then 10 vs 2 compares as the numbers ten and two, not as the characters "1" and "2". That is the one difference — everything else about the comparison stays the same.

The locale collation caveat

"Alphabetical order" is not one fixed rule once accented letters are involved. In French collation, é sorts immediately next to e; in a naive byte-by-byte comparison it can sort after z instead, because its underlying character code is higher. This tool sorts using the browser's locale-aware string comparison, which follows conventional dictionary order for accented Latin letters — but different systems and languages can still disagree on edge cases like where ß or ø belong. If a sorted list needs to match a specific national standard exactly, verify the affected entries by hand.

Questions people ask

What does "numeric" sort do with lines that aren't numbers?

Any line where a number cannot be found sorts to the end, after every line that parsed as a number, in the order it appeared. Numeric mode is meant for lists that are purely numbers or start with one, like scores or IDs — for filenames mixed with numbers, natural sort is usually what you want instead.

Is the shuffle fair?

It uses the Fisher-Yates algorithm, which gives every possible ordering of the list an equal chance, unlike naive "sort by random comparator" approaches that bias toward certain orderings. It is driven by Math.random(), which is fine for shuffling a list but not for anything security-sensitive.

Does "reverse" sort the list, or just flip it?

It flips whatever order the text is currently in — line 1 and line 20 swap positions, with nothing in between compared or reordered. To get Z→A alphabetically, use that option directly rather than sorting A→Z and then reversing, though the two happen to produce the same result for a plain sort.

Can I sort by column, like a spreadsheet?

No. This tool sorts whole lines only. For column-based data, a spreadsheet application or a script is the right tool — sorting by an inner field with plain text tools gets unreliable fast once fields vary in width.

Tidying up a list for somewhere else?

Duplicate removal, whitespace cleanup and diffing sit right next door.

See all text tools →