Locale-aware sorting (collation)

Sorting words with the alphabet rules of the active language, so names land where local readers expect instead of following character codes.

sort names correctly in every languagewhy does A with an accent sort wrongwhy is the Swedish alphabet in a different orderalphabetical order changes by countrythe customer list is alphabetical but looks wrongfile10 comes before file2 in my listlanguage-specific sortingcollationcolation

What it is

Locale-aware sorting, or collation, orders text by the alphabet rules of a chosen locale instead of by raw character codes. Swedish treats A with a ring, A with two dots, and O with two dots as separate letters placed after Z, while other languages may group accented letters with their base letter. JavaScript exposes these rules through Intl.Collator, backed by Unicode collation data.

Reach for it whenever people scan sorted names, products, cities, or headings. Create one Intl.Collator for the active locale and pass its compare function to Array.prototype.sort. Options let you choose case and accent sensitivity, ignore punctuation, or make digit runs sort numerically so file2 comes before file10. One language can even hold two official orders: German dictionaries sort u with two dots as plain u, while the phone book sorts it as ue, a difference CLDR ships as the 'de-DE-u-co-phonebk' collation.

Gotcha: lowercasing strings, stripping accents, or calling sort() with no comparator is not a substitute. Those shortcuts destroy distinctions some alphabets need and still produce the wrong order. Sorting and search also have different goals, so use a collator with usage set to 'sort' for lists rather than reusing fuzzy search normalization.

Ask AI for it

Replace every user-visible string sort with one memoized Intl.Collator(activeLocale, { usage: 'sort', numeric: true, sensitivity: 'variant' }). Sort with collator.compare instead of a default Array.prototype.sort call, lowercasing, or accent stripping. Preserve the original order when compare returns 0. Add tests for sv-SE using Z, A with a ring, A with two dots, and O with two dots, and for file2 versus file10, so the suite proves both locale collation and numeric ordering.

You might have meant

localeecmascript internationalization apilocale datalanguage negotiationnumber formatting

Go deeper