The Intl.Collator object is a constructor for collators, objects that enable language sensitive string comparison.
new Intl.Collator([locales[, options]]) Intl.Collator.call(this[, locales[, options]])
localesOptional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the locales argument, see the Intl page. The following Unicode extension keys are allowed:
co"big5han", "dict", "direct", "ducet", "gb2312", "phonebk", "phonetic", "pinyin", "reformed", "searchjl", "stroke", "trad", "unihan". The "standard" and "search" values are ignored; they are replaced by the options property usage (see below).kn"true" and "false". This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.kf"upper", "lower", or "false" (use the locale's default). This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence.optionsOptional. An object with some or all of the following properties:
localeMatcher"lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page.usage"sort" and "search"; the default is "sort".sensitivityWhich differences in the strings should lead to non-zero result values. Possible values are:
"base": Only strings that differ in base letters compare as unequal. Examples: a ≠ b, a = á, a = A."accent": Only strings that differ in base letters or accents and other diacritic marks compare as unequal. Examples: a ≠ b, a ≠ á, a = A."case": Only strings that differ in base letters or case compare as unequal. Examples: a ≠ b, a = á, a ≠ A."variant": Strings that differ in base letters, accents and other diacritic marks, or case compare as unequal. Other differences may also be taken into consideration. Examples: a ≠ b, a ≠ á, a ≠ A.The default is "variant" for usage "sort"; it's locale dependent for usage "search".
ignorePunctuationtrue and false; the default is false.numerictrue and false; the default is false. This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.caseFirst"upper", "lower", or "false" (use the locale's default); the default is "false". This option can be set through an options property or through a Unicode extension key; if both are provided, the options property takes precedence. Implementations are not required to support this property.The Intl.Collator object has the following properties and methods:
Intl.Collator.prototypeIntl.Collator.supportedLocalesOf()Collator instancesCollator instances inherit the following properties from their prototype:
Intl.Collator.prototype.compareIntl.Collator object.Intl.Collator.prototype.constructorIntl.Collator.Collator instances inherit the following methods from their prototype:
Intl.Collator.prototype.resolvedOptions()Collator
The following example demonstrates the different potential results for a string occurring before, after, or at the same level as another:
console.log(new Intl.Collator().compare('a', 'c')); // → a negative value
console.log(new Intl.Collator().compare('c', 'a')); // → a positive value
console.log(new Intl.Collator().compare('a', 'a')); // → 0
Note that the results shown in the code above can vary between browsers and browser versions. This is because the values are implementation-specific. That is, the specification requires only that the before and after values are negative and positive.
locales
The results provided by Collator.prototype.compare() vary between languages. In order to get the sort order of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument:
// in German, ä sorts with a
console.log(new Intl.Collator('de').compare('ä', 'z'));
// → a negative value
// in Swedish, ä sorts after z
console.log(new Intl.Collator('sv').compare('ä', 'z'));
// → a positive value
options
The results provided by Collator.prototype.compare() can be customized using the options argument:
// in German, ä has a as the base letter
console.log(new Intl.Collator('de', { sensitivity: 'base' }).compare('ä', 'a'));
// → 0
// in Swedish, ä and a are separate base letters
console.log(new Intl.Collator('sv', { sensitivity: 'base' }).compare('ä', 'a'));
// → a positive value
| Desktop | ||||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
| Basic support | 24 | Yes | 29 | 11 | 15 | 10 |
caseFirst option |
Yes | ? | 55 | ? | Yes | 11 |
prototype |
24 | Yes | 29 | 11 | 15 | 10 |
compare |
24 | Yes | 29 | 11 | 15 | 10 |
resolvedOptions |
24 | Yes | 29 | 11 | 15 | 10 |
supportedLocalesOf |
24 | Yes | 29 | 11 | 15 | 10 |
| Mobile | |||||||
|---|---|---|---|---|---|---|---|
| Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
| Basic support | No | 26 | Yes | 56 | ? | 10 | Yes |
caseFirst option |
Yes | Yes | ? | 56 | Yes | 11 | Yes |
prototype |
No | 26 | Yes | 56 | ? | 10 | Yes |
compare |
No | 26 | Yes | 56 | ? | 10 | Yes |
resolvedOptions |
No | 26 | Yes | 56 | ? | 10 | Yes |
supportedLocalesOf |
No | 26 | Yes | 56 | ? | 10 | Yes |
| Server | |
|---|---|
| Node.js | |
| Basic support | ? |
caseFirst option |
? |
prototype |
? |
compare |
? |
resolvedOptions |
? |
supportedLocalesOf |
? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Collator