RunTheTests
/

Sort Lines

Sort lines of text alphabetically, ascending or descending.

What you provide

How this calculation works

The tool splits your text into lines and sorts them using locale-aware comparison, so accented characters order sensibly. Pick ascending or descending.

What the results mean

Locale-aware
Accented and non-English letters sort in a natural order rather than by raw code point.
Duplicates kept
Sorting does not remove repeats — identical lines end up adjacent, which makes them easy to see. Use the duplicate remover if you want them gone.
Blank lines
Empty lines sort to the top in ascending order, since an empty string precedes everything. Trim them first if they get in the way.

Common problems and fixes

Numbers are in the wrong order
The sort is alphabetical, so "10" comes before "2" for the same reason "ab" comes before "b". Zero-pad the numbers to a fixed width — 02, 10 — and their text order matches their numeric value. This is also why filenames like report-2 and report-10 always list in the wrong order.
Capitalised lines are grouped away from lowercase ones
That is raw code point ordering, where every uppercase letter precedes every lowercase one. The sort here is locale-aware instead, which interleaves them the way a dictionary does. If you are seeing strict case grouping, the text was sorted somewhere else first.
Leading spaces are throwing the order off
A space sorts before any letter, so an indented line lands at the top regardless of its content. Run the whitespace cleaner over the text first to trim each line, then sort — the two operations in that order handle most pasted-from-a-document text.

Frequently asked questions

Why does 10 sort before 2?

Because the sort is alphabetical, and as text "1" comes before "2". To sort numbers numerically, zero-pad them (02, 10) so their text order matches their value, or use a dedicated numeric sort.

How are accented characters ordered?

By locale-aware comparison, so é sorts next to e rather than after z. Sorting by raw code point — which is what a naive implementation does — pushes every accented character to the end of the list, which is wrong in every language that uses them and looks obviously broken to anyone who reads one.

Can I sort by the second word or column?

Not here — the comparison is on the whole line from its first character. For column-based sorting, a spreadsheet is the right tool, and for structured data a spreadsheet is safer anyway: sorting a CSV as plain text reorders the header row along with everything else unless you remove it first.

Put this on your own site

Free to embed, no attribution required beyond the source link the frame carries itself. It runs entirely in your visitor's browser, sets no cookies and loads no third-party script.

Embed code
<iframe src="https://runthetests.com/embed/sort-lines/" width="100%" height="560" style="border:1px solid #e5e5e5;border-radius:8px" title="Sort Lines" loading="lazy"></iframe>

Preview it at https://runthetests.com/embed/sort-lines/. Embedded pages are marked noindex, so yours stays the canonical copy — not this one.

More in Text