Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

String.prototype.localeCompare()

这篇翻译不完整。请帮忙从英语翻译这篇文章

localeCompare() 方法返回一个数字来表明调用该函数的字符串(reference string)的排列顺序是否在某个给定的字符串的前面或者后面,或者是一样的(编码中的位置)。

新的 locales 、 options 参数能让应用程序定制函数的行为即指定用来排序的语言。  localesoptions 参数是依赖于具体实现的,在旧的实现中这两个参数是完全被忽略的。

语法

referenceStr.localeCompare(compareString[, locales[, options]])

参数

查阅浏览器支持部分来确定哪些浏览器支持 locales 参数和 options 参数, 在功能检测中检查对 localesoptions 参数的支持

compareString
用来比较的字符串
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".

ignore­Punctua­tion
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.

描述

返回一个数字来表明 referenceStr 是否出现在 compareStr 的前面或者后面,或者是相同位置的。

  • 当 referenceStr 在 compareStr 前面时返回负数
  • referenceStrcompareStr 后面时返回正数
  • 相同位置时返回0

切勿依赖于-1或1这样特定的返回值。不同浏览器之间(以及不同浏览器版本之间) 返回的正负数的值各有不同,因为W3C规范中只要求返回值是正值和负值,而没有规定具体的值。一些浏览器可能返回-2或2或其他一些负的、正的值。

示例

使用 localeCompare()

// The letter "a" is before "c" yielding a negative value
'a'.localeCompare('c'); // -2 or -1 (or some other negative value)

// Alphabetically the word "check" comes after "against" yielding a positive value
'check'.localeCompare('against'); // 2 or 1 (or some other positive value)

// "a" and "a" are equivalent yielding a neutral value of zero
'a'.localeCompare('a'); // 0

检查浏览器对扩展参数的支持

locales 和 options 参数还没有被所有阅览器所支持。检查是否被支持, 使用 "i" 参数 (a requirement that illegal language tags are rejected) 判断是否有异常 RangeError抛出:

function localeCompareSupportsLocales() {
  try {
    'foo'.localeCompare​('bar', 'i');
  } catch (e) {
    return e​.name === 'RangeError';
  }
  return false;
}

使用 locales 参数

在不同的语言下 localeCompare() 所提供的结果是不一致的。 为了能让用户得到正确的比较值, 通过使用 locales 参数来提供要比较的语言 (and possibly some fallback languages) :

console.log('ä'.localeCompare('z', 'de')); // a negative value: in German, ä sorts with a
console.log('ä'.localeCompare('z', 'sv')); // a positive value: in Swedish, ä sorts after z

使用 options 参数

localeCompare() 所提供的结果可以通过 options 参数来制定:

// in German, ä has a as the base letter
console.log('ä'.localeCompare('a', 'de', { sensitivity: 'base' })); // 0

// in Swedish, ä and a are separate base letters
console.log('ä'.localeCompare('a', 'sv', { sensitivity: 'base' })); // a positive value

性能相关

当比较大量字符串时, 比如比较大量数组时, 最好创建一个Intl.Collator 对象并使用compare 属性所提供的函数。

规范

规范 状态 说明
ECMAScript 3rd Edition (ECMA-262) Standard Initial definition. Implemented in JavaScript 1.2.
ECMAScript 5.1 (ECMA-262)
String.prototype.localeCompare
Standard  
ECMAScript 2015 (6th Edition, ECMA-262)
String.prototype.localeCompare
Standard  
ECMAScript Internationalization API 1.0 (ECMA-402)
String.prototype.localeCompare
Standard locale and option parameter definitions.

浏览器支持

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)
locales and options arguments 24 29 (29) 11 15 未实现
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes) (Yes)
locales and options arguments 未实现 26 未实现 未实现 未实现 未实现

相关链接

文档标签和贡献者

 此页面的贡献者: helloguangxue, Ende93
 最后编辑者: helloguangxue,