The cooperative’s member cards filed twice: sort() by UTF-16 code units, Intl.Collator by language, numbers and accents. Where do capitals, RT numbers and…
Why it matters
A list people read (contacts, products, file names) looks broken when sorted by code unit: capitals sneak to the top, numbers scramble, accents land at the end. A collator gives the order readers expect, in their language.
The key idea
The default sort() compares code units, not the alphabet
Common mistakes
A list of names users see looks random: capitals on top, accents at the end That is the default sort() order: UTF-16 code units, not the alphabet. For lists people read, use sort(col.compare).
Sorting a big list feels slow, though it is only sort() with a Collatorsort((a, b) => new Intl.Collator().compare(a, b)) builds a new collator for every comparison. Build it once outside, then use col.compare.
In Sweden, names with 'ä' suddenly move to the bottom of the list That is right in Swedish: Å, Ä, Ö are letters of their own after Z. Give the collator its readers’ locale; never assume accents sort the same in every language.
Try it in this playground
Turn on numeric: true In RT numbers, 'RT 2' moves in front of 'RT 10'.
Try sensitivity base, then variant In Duplicate sign-ups, watch the bracket under the cards counted as the same.
Switch the locale to sv Å, Ä, Ö move behind Z: the Swedish alphabet.
Run the speed test A new collator for every comparison is far slower than one collator reused.