Text Sorter, Sort Lines Alphabetically or by Length Free
This text sorter arranges any list of lines alphabetically, numerically, by length, or in random order, ascending or descending. Remove duplicates while sorting and copy the clean result in one click, no sign up needed.
How this text sorter handles four sort methods, including one that understands numbers
Sorting lines alphabetically is a one line call to Array.prototype.sort. The interesting engineering in a line sorter is everything wrapped around that call: a natural sort mode that stops item10 from landing before item2, a Fisher-Yates shuffle for genuine randomization, and a set of pre and post processing options that decide what counts as a line worth sorting in the first place.
Every sort runs client side in your browser the moment you click generate. Your list is never uploaded.
The four sort methods
String.prototype.localeCompare, which sorts by linguistic rules for the user’s locale rather than raw character codes. That means accented letters and mixed case sort the way a human expects, not by their underlying Unicode code point value.
a.length - b.length, sorting shortest lines first with no consideration of content.
/(\d+)|(\D+)/g, then compares chunk by chunk: numeric chunks compare as numbers, text chunks compare with localeCompare. This is what correctly orders item2 before item10, where plain alphabetical sorting would put item10 first because the character 1 sorts before 2.
sort(() => Math.random() - 0.5), which are known to be biased.
A worked sort
Input lines: item10, item2, item1
| Method | Result order |
|---|---|
| Alphabetical | item1, item10, item2 |
| Natural | item1, item2, item10 |
| Length | item1, item2, item10 (ties broken by original array order, since the comparator returns 0 for equal length) |
Pre-processing options that run before any sort
Trim and keep empty lines
Trim strips leading and trailing whitespace from every line before sorting. Keep empty lines, if turned off, filters out any line that is empty after trimming, which changes both the sort input and the final line count.
Deduplicate and case insensitive
Dedupe runs a first-seen-wins pass using a lookup object before sorting even starts. The case insensitive toggle affects two independent things: whether dedupe treats Apple and apple as the same key, and whether the sort comparator itself lowercases both sides before comparing.
Ascending or descending direction is applied last, as a plain .reverse() call on the already sorted array, for every method except random, where reversing a random order is meaningless and is skipped.
- Jeff Atwood on natural sort order is the widely cited explainer for why item2 versus item10 ordering matters to real users.
- Fisher-Yates shuffle for the unbiased shuffling algorithm this tool implements for random order.
- MDN on localeCompare for locale aware string ordering rules.
- ECMA-262 Array.prototype.sort for the stability guarantee added in ES2019.
Sorting jobs in practice
Alphabetizing a guest list or works cited page, ordering file names like chapter2.txt and chapter10.txt the way a human would expect with natural sort, sorting survey responses by length to skim the shortest first, deduplicating and alphabetizing a mailing list before an export, and randomizing a list of raffle entries or quiz questions with the shuffle mode so nobody can claim the order was rigged.
FAQ: Text Sorter
Standard alphabetical sort compares text character by character, so “item10” comes before “item2” because it compares “1” against “2” at that position and stops there. Natural sort recognizes consecutive digits as a full number and compares those numbers as values, so “item2” correctly comes before “item10.” Use natural sort for lists with version numbers, file names with numbers, or any sequence where numeric order matters.
Lines with the same character length keep their relative original order among themselves, since this tool uses a stable sort, it only reorders lines when their lengths genuinely differ. This means length sorting won’t shuffle items that happen to tie in length unpredictably.
It uses the Fisher-Yates shuffle algorithm, the standard, statistically unbiased method for randomizing a list, powered by your browser’s built in random number generator. It’s suitable for randomizing lists for raffles, random ordering, or breaking ties fairly, though it’s not intended for cryptographic security purposes.
No, case insensitivity only affects the comparison used to determine sort order, “Apple” and “apple” are treated as equivalent for ranking purposes, but each line’s original capitalization is preserved exactly as you typed it in the final result.
You can paste CSV rows in and sort them as whole lines, which works well if you’re sorting by the value that happens to be first on each line, but this tool sorts entire lines as single units, it doesn’t understand or sort by a specific column within comma separated data. For column specific spreadsheet sorting, a dedicated spreadsheet tool is a better fit.
Yes, alphabetical and natural sorting use your browser’s built in locale aware string comparison, which correctly places accented characters like é, ñ, or ü near their unaccented equivalents rather than lumping them all at the end of the alphabet, matching how dictionaries and most software sort them.
Yes, enable the “Remove duplicate lines while sorting” checkbox to deduplicate and sort in a single click, rather than running two separate tools. Duplicates are detected before sorting, so only unique lines make it into the final ordered result.
No, all sorting happens locally in your browser using JavaScript, nothing you paste is sent to a server, logged, or stored.
From the blog
Writing about writing tools
Readability scoring, keyword density and the rest, examined properly.