Line Sorter & Duplicate Remover
Sort text lines A to Z or Z to A, remove duplicate and blank lines, and fix natural number order so item2 comes before item10 — all instantly in the browser.
213 views
Why "item10" Sorts Before "item2" in a Plain Alphabetical Sort
A standard sort — the kind behind the Array.sort() method in JavaScript or the A→Z button in a spreadsheet — compares two lines character by character, left to right, using the Unicode code point of each character. It does not look at a run of digits as a whole number; it only asks whether one character is smaller or larger than another, one position at a time. That is why a list containing item1, item2, item9, item10 and item20 sorts, character by character, as item1, item10, item2, item20, item9: comparing "item10" and "item2" letter by letter, the strings are identical through "item", and the very next character decides everything — "1" versus "2". Because the character "1" is smaller than the character "2", item10 sorts before item2, even though ten is numerically larger than two. Every list where numbers appear inside text suffers from this: version numbers, filenames like image2.jpg and image10.jpg, invoice numbers, and step-by-step instructions.
Natural sort — also called numeric sort — fixes this by recognizing a run of consecutive digits as a single number and comparing its value instead of comparing digits one character at a time. The natural-sort option in this tool delegates that logic to the Intl API built into the browser, running a.localeCompare(b, undefined, {numeric: true, sensitivity: "base"}) on every pair of lines. The numeric:true flag tells the comparison to detect digit runs and treat them as numbers, so item2 correctly lands before item10, and item9 before item20 — the order a person reading the list would expect, not the order a character-by-character comparison produces.
Locale-Aware Comparison, Not Just Character Codes
Alphabetical order also depends on which language's rules are applied, and this is where a plain comparison breaks down even with no numbers involved. In English, "a" and "A" are the same letter in two cases, and accented letters such as é are usually sorted close to e. Turkish, however, treats "ı" (dotless i) and "i" (dotted i) as two separate letters with their own distinct positions in the alphabet — a distinction a raw code-point comparison, which only checks numeric Unicode values, has no way to know about. localeCompare, the same Intl method behind the natural-sort option, takes locale rules into account: with sensitivity: "base" it treats case and accent differences as equivalent for ordering purposes while still respecting the real letter order of each alphabet, whether that alphabet is English, Turkish, German or another Latin-script language.
This tool combines both concerns in one pass: choose alphabetical A→Z or Z→A as the base order, optionally layer natural numeric sorting on top so embedded numbers compare by value rather than by digit, then optionally strip duplicate lines, blank lines, and leading or trailing whitespace. Because duplicate detection compares lines as exact strings, trimming matters: "apple" and "apple " (with a trailing space) look different to a duplicate check unless the trailing space is removed first, so the trim and duplicate-removal options are built to work together rather than as unrelated switches.
Frequently Asked Questions
Why does "item10" appear before "item2" when I sort my list?
Because a plain alphabetical sort compares text character by character rather than treating digit runs as whole numbers — the character "1" is smaller than "2", so item10 wins that single-position comparison even though ten is bigger than two. Turn on natural or numeric sorting to fix it: it detects consecutive digits as one number and compares magnitude instead of individual characters.
What exactly counts as a "duplicate" line?
Two lines are duplicates only if they match exactly, character for character, including capitalization and any leading or trailing spaces — "Apple" and "apple" are treated as different lines, as are "banana" and "banana " with a trailing space. Enable the trim option before removing duplicates if whitespace differences should be ignored.
Does trimming whitespace change how many duplicates get removed?
Yes. If two otherwise identical lines differ only by leading or trailing spaces, they count as separate lines until trimmed. Checking "trim leading and trailing whitespace" normalizes both before the duplicate check runs, so those near-identical lines collapse into one.
Is localeCompare reliable for sorting non-English text, like Turkish or German?
Yes — localeCompare is a browser built-in designed specifically for locale-correct ordering, so it knows that Turkish treats dotted "i" and dotless "ı" as separate letters, and generally handles accented characters the way a reader of that language would expect, which a plain less-than comparison based on raw Unicode code points cannot do.
Does this tool upload or store my text anywhere?
No — everything runs locally in the browser using JavaScript; lines are never sent to a server, logged, or saved. Refreshing or closing the tab clears the input completely.
Similar Tools
Report a Problem
Line Sorter & Duplicate Remover
Comments
No comments yet — be the first to write one!