Onze vrijwilligers hebben dit artikel nog niet naar het Nederlands vertaald. Doe mee en help de klus te klaren!
U kunt het artikel ook in het English (US) lezen.

The Intl.Collator object is a constructor for collators, objects that enable language sensitive string comparison.

Syntax

new Intl.Collator([locales[, options]])
Intl.Collator.call(this[, 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 the options property usage (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 an options property or through a Unicode extension key; if both are provided, the options 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 an options property or through a Unicode extension key; if both are provided, the options 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 and false; the default is false.
numeric
Whether numeric collation should be used, such that "1" < "2" < "10". Possible values are true 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
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 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.

Description

The Intl.Collator object has the following properties and methods:

Properties

Intl.Collator.prototype
Allows the addition of properties to all objects.

Methods

Intl.Collator.supportedLocalesOf()
Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.

Collator instances

Properties

Collator instances inherit the following properties from their prototype:

Intl.Collator.prototype.compare
Getter; returns a function that compares two strings according to the sort order of this Intl.Collator object.
Intl.Collator.prototype.constructor
A reference to Intl.Collator.

Methods

Collator instances inherit the following methods from their prototype:

Intl.Collator.prototype.resolvedOptions()
Returns a new object with properties reflecting the locale and collation options computed during initialization of the object.

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.

Using 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

Using 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

Specifications

Specification Status Comment
ECMAScript Internationalization API 1.0 (ECMA-402)
The definition of 'Intl.Collator' in that specification.
Standard Initial definition.
ECMAScript Internationalization API 2.0 (ECMA-402)
The definition of 'Intl.Collator' in that specification.
Standard  
ECMAScript Internationalization API 4.0 (ECMA-402)
The definition of 'Intl.Collator' in that specification.
Draft  

Browser compatibility

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support 24Edge Full support YesFirefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Edge Mobile Full support YesFirefox Android Full support 56Opera Android ? Safari iOS Full support 10Samsung Internet Android Full support Yesnodejs ?
caseFirst optionChrome Full support YesEdge ? Firefox Full support 55IE ? Opera Full support YesSafari Full support 11WebView Android Full support YesChrome Android Full support YesEdge Mobile ? Firefox Android Full support 56Opera Android Full support YesSafari iOS Full support 11Samsung Internet Android Full support Yesnodejs ?
prototypeChrome Full support 24Edge Full support YesFirefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Edge Mobile Full support YesFirefox Android Full support 56Opera Android ? Safari iOS Full support 10Samsung Internet Android Full support Yesnodejs ?
compareChrome Full support 24Edge Full support YesFirefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Edge Mobile Full support YesFirefox Android Full support 56Opera Android ? Safari iOS Full support 10Samsung Internet Android Full support Yesnodejs ?
resolvedOptionsChrome Full support 24Edge Full support YesFirefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Edge Mobile Full support YesFirefox Android Full support 56Opera Android ? Safari iOS Full support 10Samsung Internet Android Full support Yesnodejs ?
supportedLocalesOfChrome Full support 24Edge Full support YesFirefox Full support 29IE Full support 11Opera Full support 15Safari Full support 10WebView Android No support NoChrome Android Full support 26Edge Mobile Full support YesFirefox Android Full support 56Opera Android ? Safari iOS Full support 10Samsung Internet Android Full support Yesnodejs ?

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown

See also

Documentlabels en -medewerkers

Laatst bijgewerkt door: wbamberg,