Chapter 06·The Intl Toolbox·~6 min·
Sorting and Alphabetical Collation
Alphabetical sort order varies by language and context. Intl.Collator implements the Unicode Collation Algorithm and CLDR tailorings for dictionary sorting, phonebook ordering, and accent-insensitive search.
Overview
Default JavaScript `.sort()` compares UTF-16 code units, which sorts `Z` before `a` and places accented letters after `z`. Alphabetical order is a cultural convention that varies by language and usage context. `Intl.Collator` implements the Unicode Collation Algorithm with CLDR language tailorings.
Core rule
Sort user-facing lists with Intl.Collator using the active locale, and use usage: 'search' with sensitivity: 'base' for locale-aware string matching.
Specification note: German sorts ä beside a, whereas Swedish sorts ä after z. A single static sort table cannot satisfy both locales.
Failure mode
Calling Array.prototype.sort() without a comparator orders strings by UTF-16 code unit value. That order places all uppercase ASCII letters before lowercase letters and places accented Latin characters after z. In a German directory sorted by code units, Öztürk appears after Zander. In Polish, ł sorts after z instead of between l and m. In a Hebrew interface, Latin strings sort before Hebrew strings unless the Hebrew locale collation is active.
Standard software sorting orders characters by binary Unicode code points, not human alphabetical order. International users cannot find sorted records.
Business stakes & user risk
In Swedish, 'Ö' sorted at the top with 'O' instead of after 'Z' makes customer lists, contact directories, and product catalogs look broken and unusable.
Rule of thumb
Sort user-facing strings using Intl.Collator with the user active locale. Never use default Array.prototype.sort().
The Novice Trap vs. Production Standard
Contrast the intuitive assumption with the architecture required for global releases.
“The default array.sort() method sorts alphabetically in all languages.”
Naive approach: Built-in JavaScript sort
names.sort(); // Compares binary code pointsDefect:Produces: ['Apple', 'Zebra', 'apple', 'Åberg']. Lowercase 'apple' and Swedish 'Åberg' appear after 'Zebra'.
Default sort compares ASCII/UTF-16 numeric values. Lowercase 'a' sorts after uppercase 'Z', and accented letters sort at the very end of the alphabet.
Production standard: Intl.Collator
names.sort(new Intl.Collator(locale, { sensitivity: "base" }).compare);Benefit:Places accented letters in true national alphabetical sequence and handles case-insensitive comparisons properly.
Cross-Functional Review Questions
Questions for product managers, designers, engineers, and testers to ask during sprint reviews.
“Is every user-visible list sorted using Intl.Collator or database collation?”
“Are search filters and autocomplete features configured for accent-insensitive matching?”
“Does alphabet index navigation (A through Z) support regional letters?”
“Are sorted tables tested with German phonebook (DIN 5007-2) and Nordic names?”
Mechanism
Multi-level collation and CLDR tailorings
JavaScript's default Array.prototype.sort() compares 16-bit UTF-16 code units in numeric order, which places every uppercase ASCII letter before every lowercase letter and places accented characters such as É (U+00C9) after z (U+007A). In contrast, the Unicode Collation Algorithm (UTS #10) evaluates strings across multiple comparison levels: primary (base letters), secondary (diacritics), and tertiary (case and width). Secondary differences break ties only when the primary base letters are identical, so Élan sorts beside elan rather than after zebra.
CLDR layers language-specific tailorings on top of the default Unicode collation table. Swedish treats å, ä, and ö as distinct letters at the end of the alphabet after z. Polish places ł between l and m. German defines two standard collation orders: dictionary order (DIN 5007-1) and phonebook order (DIN 5007-2, selected via de-DE-u-co-phonebk), which expands ä, ö, and ü to ae, oe, and ue. Passing { numeric: true } to Intl.Collator also compares multi-digit substrings by numeric value so file2 sorts before file10.
Interactive demo
Guided exercises
- 1On the default Polish list, scan the highlighted cells across the en and pl columns. `Łaba` and `Łapińska` sort with `L` under `en` but move after `Lis` under `pl`, because Polish sorts `ł` as a distinct letter between `l` and `m`.
- 2Switch to “German names · dictionary vs phonebook ordering”. Under `de-DE-u-co-phonebk`, `Mössbauer` sorts before `Mosel` and `Mütze` sorts before `Mutter` because phonebook collation expands `ö` to `oe` and `ü` to `ue`.
- 3Switch to the Swedish list. Under `sv`, `Ångström`, `Älg`, and `Östergren` move to the bottom of the list because `å`, `ä`, and `ö` are letters 27 to 29 after `z`. Under `da`, `Ångström` sorts last because the Danish alphabet ends `æ, ø, å`.
- 4Load “Case sensitivity” and compare the three English columns. Default collation places lowercase first, `-u-kf-upper` places uppercase first, and `sensitivity: 'base'` treats uppercase and lowercase as equal.
- 5Type a few entries into “Your list · comma-separated”, and include at least one accented word, e.g. Zorro, Öberg, chávez, Lukas, łódź. Each locale column re-sorts the custom entries according to its CLDR tailoring and highlights rows that diverge from the English sort order.
- 6In “Sort is not search”, keep the defaults (Search locale English, Sensitivity base, Search query muller), then switch Search locale to German (de). Under `en`, `muller` matches `Müller`. Under `de`, `muller` does not match `Müller`, whereas `mueller` matches both `Müller` and `Mueller`.
Each column sorts the list with Intl.Collator for its locale tag; highlighted cells differ from the first column, the English default. Type two or more entries to sort a custom list.
| # | en English (UCA default) | pl Polish |
|---|---|---|
| 01 | Łaba | Lewandowski |
| 02 | Łapińska | Lewiński |
| 03 | Lewandowski | Lipiński |
| 04 | Lewiński | Lis |
| 05 | Lipiński | Łaba |
| 06 | Lis | Łapińska |
| 07 | Łukasiewicz | Łukasiewicz |
| 08 | Maciejewski | Maciejewski |
| 09 | Małachowski | Małachowski |
| 10 | Modliński | Modliński |
Default order and tailorings
The Unicode Collation Algorithm (UCA) defines a default order, and CLDR adds per-locale tailorings for letter order, accents, and case. Intl.Collator applies the tailoring for its locale and returns a comparator for .sort().
- Polish ł is a separate letter that sorts between l and m. Code-unit order puts it after z.
- German phonebook (DIN 5007-2) reads ä, ö, ü as
ae,oe,ue, so Müller files with Mueller and Mössbauer sorts before Mosel. - Swedish sorts å, ä, ö after z, as the last three letters of the alphabet.
- Icelandic treats á and ó as separate letters and ends the alphabet with þ, æ, ö. Directories list people by given name, because a patronymic changes with each generation.
- Case order is tailored too: English sorts lowercase first, Danish capitals first. For most apps the right default is
sensitivity: "base"with a secondary tiebreaker.
The fix
Pass new Intl.Collator(locale).compare to every .sort() on a user-visible list.
usage: "search" loads CLDR's matching rules, which are separate from its sort rules, and sensitivity sets which differences count. A name matches when compare(query, name) === 0.
new Intl.Collator("en", { usage: "search", sensitivity: "base" })
- Müllermatch
- Muellerno match
- Möllerno match
- Núñezno match
- Åströmno match
- Célineno match
usage: "search" and an explicit sensitivity (base for a search box). Do not reuse the sort collator or strip accents with a regex.Implementation rules
- Sort user-facing strings with
Intl.Collator. Instantiate a collator with the active locale and passcollator.comparetoArray.prototype.sort(). - Separate sort collation from search collation. Use
usage: "sort"for ordered lists. Useusage: "search"withsensitivity: "base"for user filtering so queries match base characters and locale equivalences. - Enable numeric collation for alphanumeric labels. Pass
{ numeric: true }so digit sequences compare by numeric value andfile2sorts beforefile10. - Match database column collations to the application locale. Configure ICU collations in SQL databases (such as PostgreSQL
COLLATE "de-DE-x-icu") so server-sideORDER BYqueries match client-side sort order.
Operational context
Roles
Workflow stages
- ·Implementing sortable tables and directories
- ·Designing database schemas and indexes
- ·Supporting locales with tailored alphabets
See it in a market: Sweden · ä sorts after z →
Production incident
Germany standardizes two alphabetical orders under DIN 5007: DIN 5007-1 for general dictionaries (where ä sorts with a) and DIN 5007-2 for name directories (where ä sorts as ae so Müller files alongside Mueller). Default code-point sorting satisfies neither standard because it places ä after z.