The Intl.Collator
object is a constructor for collators, objects that enable language sensitive string comparison.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Syntax
new Intl.Collator([locales[, options]])
Parameters
locales
-
Optional. 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
- Variant collations for certain locales. Possible values include:
"big5han"
,"dict"
,"direct"
,"ducet"
,"gb2312"
,"phonebk"
,"phonetic"
,"pinyin"
,"reformed"
,"searchjl"
,"stroke"
,"trad"
,"unihan"
. The"standard"
and"search"
values are ignored; they are replaced by theoptions
propertyusage
(see below). kn
- Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are
"true"
and"false"
. This option can be set through anoptions
property or through a Unicode extension key; if both are provided, theoptions
property takes precedence. 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 set through anoptions
property or through a Unicode extension key; if both are provided, theoptions
property takes precedence.
options
-
Optional. 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
true
andfalse
; the default isfalse
. numeric
- Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are
true
andfalse
; the default isfalse
. This option can be set through anoptions
property or through a Unicode extension key; if both are provided, theoptions
property takes precedence. Implementations are not required to support this property. caseFirst
- Whether upper case or lower case should sort first. Possible values are
"upper"
,"lower"
, or"false"
(use the locale's default); the default is"false"
. This option can be set through anoptions
property or through a Unicode extension key; if both are provided, theoptions
property takes precedence. Implementations are not required to support this property.
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 (ECMA-402) The definition of 'Intl.Collator constructor' in that specification. |
Browser compatibility
Desktop | Mobile | Server | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Collator() constructor | Chrome Full support 24 | Edge Full support 12 | Firefox Full support 29 | IE Full support 11 | Opera Full support 15 | Safari Full support 10 | WebView Android Full support 4.4 | Chrome Android Full support 25 | Firefox Android Full support 56 | Opera Android Full support 14 | Safari iOS Full support 10 | Samsung Internet Android Full support 1.5 | nodejs
Full support
13.0.0
|
caseFirst option | Chrome Full support 24 | Edge Full support 18 | Firefox Full support 55 | IE No support No | Opera Full support 15 | Safari Full support 11 | WebView Android Full support 4.4 | Chrome Android Full support 25 | Firefox Android Full support 56 | Opera Android Full support 14 | Safari iOS Full support 11 | Samsung Internet Android Full support 1.5 | nodejs Full support 0.12 |
Legend
- Full support
- Full support
- No support
- No support
- See implementation notes.
- See implementation notes.