Intl.Collator

Intl.Collator 对象支持语言敏感的字符串比较。

¥The Intl.Collator object enables language-sensitive string comparison.

Try it

构造函数

¥Constructor

Intl.Collator()

创建一个新的 Collator 对象。

静态方法

¥Static methods

Intl.Collator.supportedLocalesOf()

返回一个数组,其中包含所提供的受支持的语言环境,而无需回退到运行时的默认语言环境。

实例属性

¥Instance properties

这些属性在 Intl.Collator.prototype 上定义并由所有 Intl.Collator 实例共享。

¥These properties are defined on Intl.Collator.prototype and shared by all Intl.Collator instances.

Intl.Collator.prototype.constructor

创建实例对象的构造函数。对于 Intl.Collator 实例,初始值为 Intl.Collator 构造函数。

Intl.Collator.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 属性的初始值为字符串 "Intl.Collator"。该属性在 Object.prototype.toString() 中使用。

实例方法

¥Instance methods

Intl.Collator.prototype.compare()

根据该 Intl.Collator 对象的排序顺序比较两个字符串的 getter 函数。

Intl.Collator.prototype.resolvedOptions()

返回一个新对象,其属性反映了在对象初始化期间计算的区域设置和排序规则选项。

示例

¥Examples

使用整理器

¥Using Collator

以下示例演示了一个字符串出现在另一个字符串之前、之后或同一级别的不同潜在结果:

¥The following example demonstrates the different potential results for a string occurring before, after, or at the same level as another:

js
console.log(new Intl.Collator().compare("a", "c")); // -1, or some other negative value
console.log(new Intl.Collator().compare("c", "a")); // 1, or some other 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

Intl.Collator.prototype.compare() 提供的结果因语言而异。为了获取应用用户界面中使用的语言的排序顺序,请确保使用 locales 参数指定该语言(可能还有一些后备语言):

¥The results provided by Intl.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:

js
// in German, ä sorts with a
console.log(new Intl.Collator("de").compare("ä", "z"));
// -1, or some other negative value

// in Swedish, ä sorts after z
console.log(new Intl.Collator("sv").compare("ä", "z"));
// 1, or some other positive value

使用选项

¥Using options

可以使用 options 参数自定义 Intl.Collator.prototype.compare() 提供的结果:

¥The results provided by Intl.Collator.prototype.compare() can be customized using the options argument:

js
// 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"));
// 1, or some other positive value

规范

Specification
ECMAScript Internationalization API Specification
# collator-objects

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also