Sort Text Lines
- 7
- Lines in
- 7
- Lines out
The sorter arranges lines of text four ways (A to Z, Z to A, shortest first or longest first) using a locale-aware comparison rather than raw character codes. That is what puts "Ångström" with the As instead of after Z, and "item 2" before "item 10".
How it works
Sorting uses Intl.Collator, the browser's own text comparison. A naive sort compares code points, which puts every capital letter before every lowercase one and files accented characters after Z, because that is where they sit in Unicode.
- Natural number ordering compares digit runs as numbers, so item 2 sorts before item 10 rather than after it.
- Case sensitivity off means "apple" and "Apple" sort together rather than in two separate blocks.
- Ignore leading spaces sorts on the visible text rather than the indentation, which matters for an indented outline.
- Sort by length is useful for finding the outliers in a list, the one truncated entry, or the one that ran long.
Blank lines are dropped rather than sorted to the top, since a block of blank lines at the start of a sorted list is never what anyone wants.
Examples
Alphabetical with accents
Lines
Zurich, Ångström, apple, Berlin
Result
Ångström, apple, Berlin, Zurich
A code-point sort gives Berlin, Zurich, apple, Ångström, capitals first, then lowercase, then anything accented. The collator gives the order a person expects.
Natural number ordering
Lines
item 1, item 2, item 10, item 20
Result
item 1, item 2, item 10, item 20
With natural ordering off you get item 1, item 10, item 2, item 20, because "1" sorts before "2" character by character.
Longest line first
Lines
A list of product descriptions
Result
Ordered by character count, descending
The fastest way to find the entries that will overflow a fixed-width field before you import them.
Frequently asked questions
Why does my list sort differently here than in a spreadsheet?
Spreadsheets and this both use locale-aware comparison, so they usually agree. Plain code-point sorts (sort in a Unix shell without a locale set, or a naive JavaScript sort) differ, putting all capitals before all lowercase and accented letters after Z.
What does "numbers in order" actually change?
It compares runs of digits as numbers rather than character by character. With it on, item 2 comes before item 10. With it off, item 10 comes before item 2, because the character 1 sorts before 2.
What happens to my blank lines?
They are dropped. Sorting them would put a block of empty lines at the top or bottom, which is never useful. If you need them preserved, remove the blank lines separately before sorting so it is a deliberate step.
How is this different from the ABC order generator?
The ABC order generator is built for alphabetising a list of words or names and handles comma-separated input. This is built for lines of text and adds length sorting, natural number ordering and control over leading whitespace.
Can it sort and remove duplicates in one pass?
Yes, turn on "remove duplicates too" and repeats are dropped before sorting. Deduplicating first is the right order, since it means the duplicate check runs on the original lines rather than on a reordered list.