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.
new Intl.Collator([locales[, options]])
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
"big5han"
,
"dict"
,
"direct"
,
"ducet"
,
"gb2312"
,
"phonebk"
,
"phonetic"
,
"pinyin"
,
"reformed"
,
"searchjl"
,
"stroke"
,
"trad"
,
"unihan"
。
"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"
,或
"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
"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"
.
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
.
默认为
"variant"
for usage
"sort"
; it's locale dependent for usage
"search"
.
ignorePunctuation
true
and
false
; the default is
false
.
numeric
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
"upper"
,
"lower"
,或
"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.
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.
| 规范 |
|---|
|
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.Collator constructor' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Collator()
构造函数
|
Chrome 24 | Edge 12 | Firefox 29 | IE 11 | Opera 15 | Safari 10 | WebView Android 4.4 | Chrome Android 25 | Firefox Android 56 | Opera Android 14 | Safari iOS 10 | Samsung Internet Android 1.5 |
nodejs
13.0.0
|
caseFirst
option
|
Chrome 24 | Edge 18 | Firefox 55 | IE No | Opera 15 | Safari 11 | WebView Android 4.4 | Chrome Android 25 | Firefox Android 56 | Opera Android 14 | Safari iOS 11 | Samsung Internet Android 1.5 | nodejs 0.12 |
完整支持
不支持
见实现注意事项。
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()