Intl.Collator
是用于语言敏感字符串比较的 collators构造函数。
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]])
Intl.Collator.call(this[, locales[, options]])
locales
可选. 缩写语言代码 (BCP 47 language tag, 例如: cmn-Hans-CN) 的字符串或者这些字符串组成的数组. 关于参数 locales 的一般形式和解释请参见Intl page. 下面的这些 Unicode 扩展键也是被允许的:
co
standard
” 和 “search
” 被忽略; 它们被 options 属性用法替换(详见下文)。kn
kf
首先排序大写或者小写。可能的值为 “upper”,“lower” 或 “false”(使用区域设置的默认值)。
此选项可以通过 options 属性或通过 Unicode 扩展 key 设置; 如果两者都提供,options 属性优先。options
可选. 包含一些或所有的下面属性的对象:
localeMatcher
"best fit"
. 有关此选项的信息, 请参见Intl page.usage
sensitivity
字符串中的哪些差异应导致结果值为非零(non-zero)。 可能的值有:
"base"
: 只有字母不同的字母比较不相等。例子: a ≠ b
, a = á
, a = A。
"accent"
: 只有不同的基本字母或重音符号和其他变音符号的字符串比较为不相等。 例如: a ≠ b
, a ≠ á
, a = A。
"case"
: 只有不同的基本字母或大小写的字符串比较不相等。 Examples: a ≠ b
, a = á
, a ≠ A。
"variant"
: 字符串的字母,口音和其他变音符号、或不同大小写比较不相等。 也可以考虑其他差异。例如: a ≠ b
, a ≠ á
, a ≠ A
."variant" 的默认值使用
"sort"
; 它的 locale 依赖于使用 "search"
.
ignorePunctuation
numeric
caseFirst
首先排序大写或者小写。可能的值为 “upper”,“lower” 或 “false”(使用区域设置的默认值)。
此选项可以通过 options 属性或通过 Unicode 扩展 key 设置; 如果两者都提供,options 属性优先。实现不需要支持此属性。Intl.Collator
类有一下属性和方法
Intl.Collator.prototype
Intl.Collator.supportedLocalesOf()
Collator 实例
Collator 实例从其原型继承以下属性:
Intl.Collator.prototype.constructor
Intl.Collator
.Intl.Collator.prototype.compare
Intl.Collator
object.Intl.Collator.prototype.resolvedOptions()
以下示例演示在另一个之前,之后或同级别发生的字符串的不同潜在结果:
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
请注意,上述代码中显示的结果可能会因浏览器和浏览器版本而异。 这是因为值是实现特定的。 也就是说,规范仅需要前后值为负和正。
locales
Collator.prototype.compare()
提供的结果在不同语言之间有所不同。为了获得用于您的应用程序的用户界面的语言格式,请确保设定了语言(可能还有一些回退语言)参数:
// 德语中, ä 使用 a 的排序
console.log(new Intl.Collator('de').compare('ä', 'z'));
// → 一个负值
// 瑞典语中, ä 在 z 的后面
console.log(new Intl.Collator('sv').compare('ä', 'z'));
// → 一个正值
options
Collator.prototype.compare()
提供的结果可以使用 options 参数自定义:
// 德语中, ä 使用 a 作为基本字母
console.log(new Intl.Collator('de', { sensitivity: 'base' }).compare('ä', 'a'));
// → 0
// 瑞典语中, ä 和 a 是单独的基本字母
console.log(new Intl.Collator('sv', { sensitivity: 'base' }).compare('ä', 'a'));
// → 一个正值
Specification | Status | Comment |
---|---|---|
ECMAScript Internationalization API 1.0 (ECMA-402) Intl.Collator |
Standard | Initial definition. |
ECMAScript Internationalization API 2.0 (ECMA-402) Intl.Collator |
Standard | |
ECMAScript Internationalization API 4.0 (ECMA-402) Intl.Collator |
Draft |
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Collator | 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 No support No | Chrome Android Full support 26 | Edge Mobile Full support Yes | Firefox Android Full support 56 | Opera Android ? | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs ? |
caseFirst option | Chrome Full support Yes | Edge ? | Firefox Full support 55 | IE ? | Opera Full support Yes | Safari Full support 11 | WebView Android No support No | Chrome Android Full support Yes | Edge Mobile ? | Firefox Android Full support 56 | Opera Android Full support Yes | Safari iOS Full support 11 | Samsung Internet Android Full support Yes | nodejs ? |
prototype | Chrome Full support 24 | Edge Full support Yes | Firefox Full support 29 | IE Full support 11 | Opera Full support 15 | Safari Full support 10 | WebView Android No support No | Chrome Android Full support 26 | Edge Mobile Full support Yes | Firefox Android Full support 56 | Opera Android ? | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs ? |
compare | Chrome Full support 24 | Edge Full support Yes | Firefox Full support 29 | IE Full support 11 | Opera Full support 15 | Safari Full support 10 | WebView Android No support No | Chrome Android Full support 26 | Edge Mobile Full support Yes | Firefox Android Full support 56 | Opera Android ? | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs ? |
resolvedOptions | Chrome Full support 24 | Edge Full support Yes | Firefox Full support 29 | IE Full support 11 | Opera Full support 15 | Safari Full support 10 | WebView Android No support No | Chrome Android Full support 26 | Edge Mobile Full support Yes | Firefox Android Full support 56 | Opera Android ? | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs ? |
supportedLocalesOf | Chrome Full support 24 | Edge Full support Yes | Firefox Full support 29 | IE Full support 11 | Opera Full support 15 | Safari Full support 10 | WebView Android No support No | Chrome Android Full support 26 | Edge Mobile Full support Yes | Firefox Android Full support 56 | Opera Android ? | Safari iOS Full support 10 | Samsung Internet Android Full support Yes | nodejs ? |