Advertisement
Advanced JavaScript, Date, Time & Internationalization (Intl API)

How do you sort an array of strings correctly for a given language using Intl.Collator?

Short answer

Default string comparison ( ) uses UTF-16 code point order, which doesn't match human-expected alphabetical order for many languages (accents, special characters). Intl.Collator provides locale-aware, linguistically correct sorting.

Save JavaScript mock interview
Advertisement
AModel answer
Default string comparison (< and >) uses UTF-16 code point order, which doesn't match human-expected alphabetical order for many languages (accents, special characters). Intl.Collator provides locale-aware, linguistically correct sorting.
const collator = new Intl.Collator('sv');
['ö', 'z', 'a'].sort(collator.compare); // locale-correct Swedish order
Real-world example
Sorting a list of customer names correctly for users in different countries, respecting local alphabetization rules.
Likely follow-ups
  • How does Intl.Collator handle case-insensitive sorting?
Related topics