How to Sort Lines and Remove Duplicates from a List

Last updated: · 5 min read

Sorting a mailing list, a column of product codes or a set of place names by hand is slow and easy to get wrong. Many quick scripts are not much better, because they order characters by their code number instead of by the alphabet: every capital letter lands before every lowercase one, and a word starting with an accented letter drops below Z. Proper alphabetical order is a language rule, not a property of the characters, and it differs between languages. This guide explains exactly what each of the nine operations in Line Tools does, how language-aware sorting works, and the mistakes that produce surprising results. The outputs in the example come from the tool itself.

Formula

The text is split at line breaks (Windows, Unix and old Mac endings are all recognized); each line is one item.

1  Sort A → Z            by the alphabet of the page language
2  Sort Z → A            the same rule, reversed
3  Sort numerically      by the number at the start of the line, smallest first
4  Sort by length        by character count, shortest first
5  Remove duplicates     the first of each set of identical lines is kept
6  Remove empty lines    lines containing only spaces are removed too
7  Reverse line order    the last line becomes the first
8  Trim                  spaces at the start and end of each line are removed
9  Number the lines      adds 1. 2. 3. …

Alphabetical sorting uses Intl.Collator, which implements the Unicode Collation Algorithm (UTS #10) with the language-specific rules from CLDR. The algorithm compares strings in levels: base letters first, then accents, then case. In English an accented letter is a variant of its base letter, so é sorts with e and ö with o. Other languages tailor this: Spanish treats ñ as a separate letter after n, and Turkish treats ç, ö and ş as separate letters that follow c, o and s. The tool sorts by the language of the page you are on, so the same list can come out in a different order on another language version of the site.

What the inputs mean

Text (one item per line)
The list to process, one item per line. A single line break at the very end is not counted as an extra empty line. Up to 100,000 lines and 1,000,000 characters are handled.
Operation
One of the nine operations. The tool applies one at a time; to chain them, for example remove duplicates and then sort, feed the result back in as the input.

Worked example: Cleaning and sorting a list of eight city names

  1. Input, one per line: Oslo, İzmir, Çorum, Zürich, Ceyhan, Iğdır, Örebro, Çorum.
  2. Choose “Remove duplicate lines”. The second Çorum is dropped and the remaining order is untouched: Oslo, İzmir, Çorum, Zürich, Ceyhan, Iğdır, Örebro.
  3. Feed those seven lines back in and choose “Sort A → Z”. On the English page the output is: Ceyhan, Çorum, Iğdır, İzmir, Örebro, Oslo, Zürich.
  4. Why this order? Accents are ignored on the first pass, so Çorum is compared as “Corum” and Örebro as “Orebro”. “Ce” comes before “Co”, and “Or” comes before “Os”, which puts Örebro ahead of Oslo.
  5. On the Turkish version of the site the same list ends with Oslo, Örebro, Zürich, because in the Turkish alphabet ö is a separate letter that comes after o.
  6. A second test with numbers: the lines 10, 9, 100 become 10, 100, 9 with “Sort A → Z” (compared as text) and 9, 10, 100 with “Sort numerically”.

Result: Tool output on the English page: Ceyhan, Çorum, Iğdır, İzmir, Örebro, Oslo, Zürich, each on its own line. The duplicate is gone and accented letters sit next to their base letters instead of after Z.

📑 Line Tools (Sort, Remove Duplicates)

Free, no sign-up; your data stays in your browser.

Open the calculator →

How to read the result

Which order is “correct” depends on who will read the list: an English index files accented names under their base letter, while a Swedish or Turkish reader expects ö in a different place. Case only matters as a tie-breaker in sorting; when two lines are otherwise equal, the lowercase one comes first, so apple precedes Apple. Duplicate removal is deliberately strict and treats lines as equal only when they match character for character. The numeric and length sorts are stable, meaning lines with the same value keep their original relative order, which makes two-step sorts predictable.

Common mistakes

  • Sorting numbers with A → Z. In a text sort 100 comes before 9 because comparison starts at the first character; use “Sort numerically” for lists of numbers.
  • Removing duplicates before trimming. “apple” and “apple ” with a trailing space are different lines; run “Trim leading and trailing spaces” first, then “Remove duplicate lines”.
  • Expecting duplicates to be matched without regard to case. “Apple” and “apple” are both kept; the tool does not change capitalization.
  • Overlooking that the numeric sort reads only a number at the start of the line. “3 pears” sorts as 3, while “pears 3” has no leading number; such lines go to the end in their original order.
  • Entering numbers with thousands separators. 1,250 is read as 1.25, because both the comma and the period are accepted as decimal marks; remove the separators first.
  • Assuming the order is the same in every language. The sort follows the language of the page, so open the site in the language whose alphabet the list should follow.

Frequently asked questions

Why does 10 come before 9 when I sort?

Because an alphabetical sort compares text character by character, and “1” comes before “9”. Choose the numeric sort to compare the values instead; it also handles negative numbers and decimals.

Which line is kept when duplicates are removed?

The first occurrence. The remaining lines keep their order, and the list does not need to be sorted first; repeats are found even when other lines sit between them.

Is duplicate removal case-sensitive?

Yes. Lines must be identical, including capitalization and spaces, to count as duplicates.

How are accented letters such as é or ö sorted?

On the English page they sort with their base letter: résumé sits next to resume, and Örebro comes before Oslo. A plain code-point sort would push them after z instead.

Do uppercase letters sort before lowercase?

No. Letters are ordered alphabetically regardless of case, and only identical words are separated by case, lowercase first: a, A, b, B.

How many lines can the tool handle?

Up to 100,000 lines and 1,000,000 characters in total. Beyond that it shows a message instead of processing the text.

The result is not exact or binding and is for information only; it is not an official document and cannot be used in official procedures. Confirm it with the competent authority or a professional before acting; no liability is accepted for losses arising from its use.

Sources

Choose your cookie preference to continue

Records the site needs to work (theme, language, favourites) stay in your browser.

This site is free and funded by ads.

With your consent, Google Analytics cookies measure visits and Google AdSense cookies are used to show ads; the data is processed on Google servers abroad.

If you reject, the site works just the same.

Cookie Policy · KVKK notice