Intl.Collator() constructor
The Intl.Collator() constructor creates Intl.Collator objects that enable language-sensitive string comparison.
Syntax
new Intl.Collator() new Intl.Collator(locales) new Intl.Collator(locales, options)
Parameters
-
localesOptional -
Optional. A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the
localesargument, see the Intl page.The following Unicode extension keys are allowed:
Note: These keys can usually also be set with
options(as listed below). When both are set, theoptionsproperty takes precedence.co-
Variant collations for certain locales. Possible values include:
big5hancompatdictdirectduceteorgb2312-
phonebk(only supported in German) phoneticpinyinreformedsearchjlstroketradunihanzhuyin
Note: This option can be also be set through the
optionsproperty "collation". kn-
Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are "
true" and "false". This option can be also be set through theoptionsproperty "numeric". kf-
Whether upper case or lower case should sort first. Possible values are "
upper", "lower", or "false" (use the locale's default). This option can be also be set through theoptionsproperty "caseFirst".
-
optionsOptional -
An object with some or all of the following properties:
localeMatcher-
The locale matching algorithm to use. Possible values are "
lookup" and "best fit"; the default is "best fit". For information about this option, see the Intl page. usage-
Whether the comparison is for sorting or for searching for matching strings. Possible values are "
sort" and "search"; the default is "sort". sensitivity-
Which 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". - "
ignorePunctuation-
Whether punctuation should be ignored. Possible values are
trueandfalse; the default isfalse. numeric-
Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are
trueandfalse; the default isfalse.Note: This option can also be set through the
knUnicode extension key; if both are provided, thisoptionsproperty takes precedence. caseFirst-
Whether upper case or lower case should sort first. Possible values are "
upper", "lower", or "false" (use the locale's default). This option can be set through anoptionsproperty or through a Unicode extension key; if both are provided, theoptionsproperty takes precedence.Note: This option can also be set through the
kfUnicode extension key; if both are provided, thisoptionsproperty takes precedence. collation-
Variant collations for certain locales. Possible values include:
big5hancompatdictdirectduceteorgb2312-
phonebk(only supported in German) phoneticpinyinreformedsearchjlstroketradunihanzhuyin
Note: This option can also be set through the
coUnicode extension key; if both are provided, thisoptionsproperty takes precedence.
Examples
Using 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.
Specifications
| Specification |
|---|
| ECMAScript Internationalization API Specification (ECMAScript Internationalization API) # sec-the-intl-collator-constructor |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
Collator |
24 |
12 |
29 |
11 |
15 |
10 |
4.4 |
25 |
56 |
14 |
10 |
1.5 |
options_caseFirst_parameter |
24 |
18 |
55 |
No |
15 |
11 |
4.4 |
25 |
56 |
14 |
11 |
1.5 |
options_collation_parameter |
87 |
87 |
85 |
No |
73 |
14.1 |
87 |
87 |
85 |
No |
14.5 |
14.0 |
See also
© 2005–2021 MDN 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/Intl/Collator/Collator