Sort Lines Alphabetically

Published Updated

Sort lines A to Z, Z to A or by number, with a case-insensitive option. Plain A to Z puts every capital before every lowercase letter and Äpfel after zebra, while numeric mode reads digit runs as values, so item2 comes before item10.

The same eight words in three settings

The input was banana, Apple, cherry, apple, Äpfel, zebra, éclair and Zoo. Google Chrome 156 in an English locale gave:

SettingResult
A to Z, case-sensitiveApple, Zoo, apple, banana, cherry, zebra, Äpfel, éclair
A to Z, case-insensitiveÄpfel, Apple, apple, banana, cherry, éclair, zebra, Zoo
Z to AThe A to Z order reversed

The case-insensitive rule is localeCompare, which MDN says takes the language from the browser unless told otherwise.

The same lists in four runtimes

Thirteen generated lists were sorted with the page's own code, each in the six settings (A to Z, Z to A and Numeric, with the case option off and on), which is 78 sorts. They ran in Google Chrome 158, Firefox 155, Playwright's WebKit build (not the Safari app) and Node.js 22.23.1, each with an English default language. The 78 measured results were identical in all four runtimes. Three of the lists:

Input linesA to Z, case-sensitiveA to Z, case-insensitive
résumé, resume, Resume, résume, Résumé, naïve, naive, Naïve, café, cafe, Cafe, caffCafe, Naïve, Resume, Résumé, cafe, caff, café, naive, naïve, resume, résume, résumécafé, cafe, Cafe, caff, naïve, naive, Naïve, résumé, resume, Resume, résume, Résumé
a b, ab, a-b, a_b, a.b, a,b, a'b, a/b, A B, a b, a, a , (a), [a], a1, a# a, (a), A B, [a], a , a b, a b, a#, a'b, a,b, a-b, a.b, a/b, a1, a_b, ab a, (a), [a], a , a b, a b, A B, a_b, a-b, a,b, a.b, a'b, a/b, a#, a1, ab
😀, a, Z, é, 日本, 한국, α, я, €, 1, #, ~, ß, ss, æ, ae, fi, fi#, 1, Z, a, ae, fi, ss, ~, ß, æ, é, α, я, €, 日本, 한국, 😀, fi#, ~, 😀, €, 1, a, æ, ae, é, fi, fi, ß, ss, Z, α, я, 한국, 日本

The ECMA-402 specification describes the sensitivity setting that the case option uses: strings with the same base letters compare as equal whatever their case or accents. In the third row, ß, æ and fi also tie with ss, ae and fi, and tied lines keep the order they were typed.

Code-unit order against collation order

The case-sensitive sort compares UTF-16 code units, and the case-insensitive sort uses collation. They disagree on text that can be written in more than one way. The decomposed é (e followed by U+0301) and the composed é (U+00E9) tie under collation and land in different places under code units. In the symbols row above, the emoji U+1F600 also comes before fi (U+FB01) in the case-sensitive column, because its first code unit is a surrogate that sorts below U+FB01.

Input linesA to Z, case-sensitiveA to Z, case-insensitive
é, e\u0301, f, e, É, E\u0301E\u0301, e, e\u0301, f, É, éé, e\u0301, e, É, E\u0301, f

The Unicode collation standard, UTS #10 says that canonically equivalent sequences must sort the same and that the position of characters in the code charts does not specify their sort order.

How numeric mode reads a line

Each line is cut into runs of digits and runs of other characters, and the runs are compared in turn. Digit runs are compared as whole numbers, however long. A sample of 15 lines came out as: an empty line, -10, -5, 3, 3.5, 3.10, 007, 7, 9, 10, file02, file2, item1, item2 and item10.

That makes file names with numbers sort the way people expect, with item2 before item10.

Ties and Z to A

Z to A is the chosen A to Z order turned around. Lines that compare as equal, such as Apple and apple with the case option on, keep their input order in A to Z and appear in the opposite order in Z to A. The sort never removes or merges lines.

What the browser language changes

The case-insensitive sort asks the browser for its language and orders by that language's rules. The same ten names, sorted with the browser language set to German and to Swedish in Chrome 158, Firefox 155 and the WebKit build (all three gave the same order), compare with the default English order like this:

Browser languageA to Z, case-insensitive
English (each runtime's default)Ångström, apple, Äpple, Ärlig, Åsa, Öl, Ola, Oscar, Zebra, zoo
de-DEÅngström, apple, Äpple, Ärlig, Åsa, Öl, Ola, Oscar, Zebra, zoo
sv-SEapple, Ola, Oscar, Zebra, zoo, Ångström, Åsa, Äpple, Ärlig, Öl

In Swedish, Å, Ä and Ö are separate letters after Z. MDN shows the same difference between German and Swedish for ä. With the case-insensitive box unticked, all three modes gave the same order in all three languages; Numeric with the box ticked follows the language too.

Speed on long lists

Time in milliseconds to sort generated lists of random words, median of 3 runs. The 100,000-line list is 994,306 characters.

Generated list and settingNode.jsChrome 158Firefox 155WebKit build
20,000 lines, A to Z109106
20,000 lines, A to Z, case-insensitive30294122
20,000 lines, Numeric71479560
20,000 lines, Numeric, case-insensitive946910475
100,000 lines, A to Z64506638
100,000 lines, A to Z, case-insensitive200174299126
100,000 lines, Numeric489290573339
100,000 lines, Numeric, case-insensitive6164701,304422

Sorting a list

  1. Step 1

    Paste the lines

    One entry per line. Blank lines are sorted too: they land at the top in A to Z and numeric modes, and at the bottom in Z to A.

  2. Step 2

    Pick the order

    Choose Alphabetical, Reverse alphabetical or Numeric, and tick the case-insensitive box if capitals should not matter.

  3. Step 3

    Copy Result

    The sorted list appears in the second box and updates as you type. Clear empties both.

Related list tools

Sorting questions

Why does Zebra come before apple?

Plain A to Z compares character codes, and every capital letter has a lower code than every lowercase one. Tick the case-insensitive box to get apple before Zebra.

Does the case-insensitive option sort accents correctly?

It uses the browser's locale comparison, so Äpfel sits with the A words in an English browser. The language comes from the browser, so a machine set to another language can order accented letters differently.

How are negative numbers sorted in numeric mode?

A line that starts with a minus sign right before a digit is a negative number, so -10 comes before -5, and both come before 0 and the positive numbers. A hyphen anywhere else, as in item-2, is ordinary text.

Why is 3.5 before 3.10?

Each digit run is its own number, so 3.5 reads as 3, a dot and 5, against 3, a dot and 10. That is right for version numbers and wrong for decimals.

What happens to equal numbers such as 7 and 007?

They tie on value, and the longer text decides, so 007 comes before 7. Digit runs of any length work, and 99999999999999999999 sorts after 9999999999999999999 correctly.

Is the list uploaded?

No. Sorting is done by JavaScript in the page, and nothing leaves the tab.

Why do two spellings of é end up in different places?

The case-sensitive sort compares code units, so é as one character (U+00E9) and é as e plus a combining accent (U+0301) are different strings and sort apart. With the case-insensitive box ticked, the two tie and stay together in input order. In the test, f and É sat between the two forms in the case-sensitive order.

Does my browser language change the order?

Only with the case-insensitive box ticked. With the language set to Swedish, Ångström, Åsa, Äpple, Ärlig and Öl came after zoo in Chrome 158, Firefox 155 and the WebKit build. With German or English they sorted with the A and O words. The case-sensitive order did not change.

Are ß and ss, or æ and ae, treated as equal?

With the case-insensitive box ticked, yes. ß and ss tie, and so do æ and ae and fi and fi, so each pair stays in the order you typed it. In the case-sensitive sort they are different strings.

How long does a list of 100,000 lines take?

For a generated list of random words, A to Z took a median of 50 ms in Chrome 158, 66 ms in Firefox 155 and 38 ms in the WebKit build. With the case-insensitive box ticked it took 174, 299 and 126 ms, and numeric mode with the box ticked took 470, 1,304 and 422 ms.

Related Tools