一般的字符串比较都是直接比较unicode的大小,这个方法可以用于比较不同地区的语言的字符串。比如中文按照拼音排序。
localeCompare()介绍
语法参数
localeCompare(compareString)
localeCompare(compareString, locales)
localeCompare(compareString, locales, options)
其中,新的 locales 和 options 参数,允许使用者特定语言环境和选项,使期望更加准确。在老版本中会忽略不同语言之间的差异。
参数
compareString:进行比较的字符串。
locales:
co
不同的语言,常用的值有:
- big5han (Chinese; not available in Chrome or Edge)
- compat (Arabic)
- dict (Sinhala)
- direct (deprecated, do not use)
- ducet (not available, do not use)
- emoji (root)
- eor (root)
- gb2312 (Chinese; not available in Chrome or Edge)
- phonebk(German)
- phonetic (Lingala)
- pinyin (Chinese)
- reformed (Swedish; do not specify explicitly as this is the default for Swedish)
- searchjl (Korean; do not use for sorting)
- stroke (Chinese)
- trad
- unihan (Chinese, Japanese, and Korean; not available in Chrome or Edge)
- zhuyin (Chinese)
其他的参数和规则可以看 Intl 国际化对象中的描述,其实localeCompare方法中的locale就是通过Intl.Collator()实现的.:
kn
是否应使用数字排序规则,例如“1”<“2”<“10”。 可能的值为“真”和“假”。 这个选项也可以通过选项属性“numeric”来设置。
kf
大写还是小写应该先排序。 可能的值为“upper”、“lower”或“false”(使用区域设置的默认值)。 也可以通过选项属性“caseFirst”设置此选项。
locales参数示例
"伯阳".localeCompare("符舍","pinyin") // 按中文拼音排序
// 或者
"伯阳".localeCompare("符舍","zh") // 按中文拼音排序
在数组中排序:
let arr = ["马哲","毛概","阿波","伯伯"]
let result = arr.sort((p1,p2)=>{
return p1.localeCompare(p2,"zh")
})
console.log(result) // ['阿波', '伯伯', '马哲', '毛概']
options:具有以下部分或全部属性的对象:
localeMatcher:
要使用的语言环境匹配算法。可能的值是“lookup”和“best fit”;默认为“best fit”。有关此选项的信息,请参阅 Intl 页面。
usage:
比较是用于排序还是用于搜索匹配的字符串。可能的值是”sort”(排序) 和”search”(搜索);默认为”sort”(排序)。
sensitivity:
字符串中的哪些差异应导致非零结果值。可能的值为:
- “base”:只有基本字母不同的字符串比较不相等。示例:a ≠ b、a = á、a = A。
- “accent”:只有在基本字母或重音符号和其他变音符号不同的字符串比较为不相等。示例:a ≠ b、a ≠ á、a = A。
- “case”:只有基本字母或大小写不同的字符串比较不相等。示例:a ≠ b、a = á、a ≠ A。
- “variant”:基本字母、重音符号和其他变音符号不同的字符串,或者大小写比较不相等的字符串。也可以考虑其他差异。示例:a ≠ b、a ≠ á、a ≠ A。
对于usage设置为”sort”时,默认为”variant”;它依赖于usage为”search”的语言环境。
ignorePunctuation:
是否应该忽略标点符号。可能的值为true和false;默认为false。
numeric:
是否应使用数字排序规则,例如“1”<“2”<“10”。可能的值为true和false;默认为false。
caseFirst:
大写优先还是小写优先。 可能的值为“upper”、“lower”或“false”(默认使用当前语言地区的习惯)。 可以通过选项属性或通过 Unicode 扩展键设置此选项; 如果两者都提供,则选项属性优先。
collation:
选择语种,等同于上面的locales属性。 可能的值包括:
- big5han (Chinese; not available in Chrome or Edge)
- compat (Arabic)
- dict (Sinhala)
- direct (deprecated, do not use)
- ducet (not available, do not use)
- emoji (root)
- eor (root)
- gb2312 (Chinese; not available in Chrome or Edge)
- phonebk(German)
- phonetic (Lingala)
- pinyin (Chinese)
- reformed (Swedish; do not specify explicitly as this is the default for Swedish)
- searchjl (Korean; do not use for sorting)
- stroke (Chinese)
- trad
- unihan (Chinese, Japanese, and Korean; not available in Chrome or Edge)
- zhuyin (Chinese)
实例
```javascript console.log(‘ä’.localeCompare(‘z’, ‘de’)); // a negative value: in German, ä sorts before z console.log(‘ä’.localeCompare(‘z’, ‘sv’)); // a positive value: in Swedish, ä sorts after z
```javascript
// 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
// by default, "2" > "10"
console.log("2".localeCompare("10")); // 1
// numeric using options:
console.log("2".localeCompare("10", undefined, { numeric: true })); // -1
// numeric using locales tag:
console.log("2".localeCompare("10", "en-u-kn-true")); // -1
用Intl对象实现
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