What You'll Learn
You'll learn how to create an Intl.Collator instance with specific options that control how strings are compared and sorted according to different languages and use cases.
Understanding Intl.Collator
The Intl.Collator constructor creates a collator object — a specialized tool for locale-aware string comparison. Think of it as a multilingual referee that knows the sorting rules for different languages and can judge which string comes first, or whether two strings should be considered equal.
Unlike simple character-by-character comparison ("a" < "b"), Collator respects linguistic conventions: German treats "ä" differently than English does, and Swedish sorts "ö" in a unique position.
Constructor Syntax
const collator = new Intl.Collator(locale, options);
Locale: A BCP 47 language tag like 'en-US', 'de-DE', or an array of locales.
Options object (all optional):
usage:'sort'(default) for sorting lists, or'search'for matching stringssensitivity: Controls how strict the comparison is:'base': Ignore accents and case (a = á = A)'accent': Consider accents, ignore case (a ≠ á, buta = A)'case': Consider case, ignore accents (a = á, buta ≠ A)'variant': Consider everything (strictest)
numeric:truetreats numeric substrings as numbers ('2' < '10')
const sortCollator = new Intl.Collator('en', {
usage: 'sort',
sensitivity: 'variant'
});
const searchCollator = new Intl.Collator('en', {
usage: 'search',
sensitivity: 'base'
});
When to Use Each Option
usage: 'sort': Precise ordering for listsusage: 'search': Flexible matching (e.g., finding "resume" when user types "résumé")numeric: true: Natural sorting for filenames or version numbers
Key Takeaway: Intl.Collator options let you control comparison strictness and behavior, tailoring string comparison to your specific use case — whether sorting data precisely or matching search queries flexibly.