JavaScript Reference · Date and Intl
The Intl.Collator object
Reference for Intl.Collator: compare and sort strings by locale rules, with natural number order, case and accent sensitivity and fast reusable comparators.
Intl.Collator compares strings the way people in a given language expect them to be ordered. The default array.sort() compares UTF-16 code units, so every uppercase letter sorts before every lowercase letter and accented letters land after "z". A collator knows that "a", "A" and "ä" belong together near the start of the alphabet.
A collator's compare property is a function that returns a negative number, zero or a positive number, which is exactly what sort() needs. Build the collator once and reuse it.
const collator = new Intl.Collator("en"); ["b", "a", "ä", "Z"].sort(collator.compare); // ["a", "ä", "b", "Z"]
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
new Intl.Collator() new Intl.Collator(locales) new Intl.Collator(locales, options) collator.compare(a, b) collator.resolvedOptions()
| Name | Description |
|---|---|
locales |
A language tag, an array of tags, or undefined for the default locale. |
options.usage |
"sort" (default) or "search". |
options.sensitivity |
"base" (only letter differences count), "accent" (letters and accents), "case" (letters and case) or "variant" (everything; the default for sorting). |
options.numeric |
true compares digit sequences as numbers, so "2" comes before "10". Default false. |
options.caseFirst |
"upper", "lower" or "false" (locale default). |
options.ignorePunctuation |
true skips spaces and punctuation. Default false (true in Thai). |
options.collation |
A special ordering such as "phonebk" in German, where supported. |
Return value: compare(a, b) returns a negative number if a sorts before b, 0 if they are equal under the chosen sensitivity, and a positive number if a sorts after b. Only the sign is meaningful.
Exceptions: a RangeError is thrown for an invalid locale tag or option value.
Usage notes
- Pass
collator.comparedirectly tosort(). It is already bound, so no wrapper function is needed. localeCompare()uses the same engine.a.localeCompare(b, locale, options)is convenient for a one-off comparison, but a reusable collator is faster when sorting large arrays.- Language matters. In Swedish, "ä" sorts after "z"; in German, it sorts next to "a".
- Equal does not mean identical. With
sensitivity: "base",compare("a", "A")is0. Two different strings can compare as equal, which makes the collator good for case-insensitive matching. - Natural sorting: use
numeric: truefor names like "item 9" and "item 10", file names and version-like labels. - Sorting objects:
people.sort((x, y) => collator.compare(x.name, y.name)). sort()mutates. UsetoSorted()or copy with[...array]first when you must keep the original order.- Stable order: modern engines sort stably, so ties keep their original order.
- Results can differ across environments because locale data differs; do not rely on exact order for obscure locales in tests.
Browser support
Supported in all current browsers and in Node.js. Option and collation support varies slightly between engines.