String.prototype.localeCompare()
localeCompare() 方法用于 String 值,它返回一个数字,指示此字符串在排序顺序中是出现在给定字符串之前、之后还是与其相同。在支持 Intl.Collator API 的实现中,此方法会委托给 Intl.Collator。
在比较大量字符串时,例如对大型数组进行排序,最好创建一个 Intl.Collator 对象,并使用其 compare() 方法提供的函数。
试一试
const a = "réservé"; // With accents, lowercase
const b = "RESERVE"; // No accents, uppercase
console.log(a.localeCompare(b));
// Expected output: 1
console.log(a.localeCompare(b, "en", { sensitivity: "base" }));
// Expected output: 0
语法
localeCompare(compareString)
localeCompare(compareString, locales)
localeCompare(compareString, locales, options)
参数
locales 和 options 参数自定义函数行为,并允许应用程序指定应使用的语言的格式约定。
在支持 Intl.Collator API 的实现中,这些参数与 Intl.Collator() 构造函数的参数完全对应。不支持 Intl.Collator 的实现应忽略这两个参数,使得返回的比较结果完全依赖于实现——只要求保持一致。
compareString-
要与
referenceStr进行比较的字符串。所有值都会被 强制转换为字符串,因此省略它或传入undefined会导致localeCompare()与字符串"undefined"进行比较,这通常不是您想要的。 locales可选-
包含 BCP 47 语言标签的字符串,或此类字符串的数组。对应于
Intl.Collator()构造函数的locales参数。在不支持
Intl.Collator的实现中,此参数将被忽略,通常会使用主机的语言环境。 options可选-
一个用于调整输出格式的对象。对应于
Intl.Collator()构造函数的options参数。在不支持
Intl.Collator的实现中,此参数将被忽略。
有关 locales 和 options 参数的详细信息以及如何使用它们,请参阅 Intl.Collator() 构造函数。
返回值
如果 referenceStr 出现在 compareString 之前,则返回一个负数;如果 referenceStr 出现在 compareString 之后,则返回正数;如果它们相等,则返回 0。
在支持 Intl.Collator 的实现中,这等同于 new Intl.Collator(locales, options).compare(referenceStr, compareString)。
描述
返回一个整数,指示 referenceStr 出现在 compareString 之前、之后还是与之等效。
- 当
referenceStr出现在compareString之前时为负数 - 当
referenceStr出现在compareString之后时为正数 - 相等时返回
0
警告:不要依赖于 -1 或 1 的精确返回值!
负数和正数整数结果在不同浏览器(以及不同浏览器版本)之间会有所不同,因为 ECMAScript 规范仅强制要求负数和正数值。某些浏览器可能会返回 -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
排序数组
localeCompare() 可以实现数组的忽略大小写排序。
const items = ["réservé", "Premier", "Cliché", "communiqué", "café", "Adieu"];
items.sort((a, b) => a.localeCompare(b, "fr", { ignorePunctuation: true }));
// ['Adieu', 'café', 'Cliché', 'communiqué', 'Premier', 'réservé']
检查浏览器对扩展参数的支持
并非所有浏览器都支持 locales 和 options 参数。
要检查实现是否支持它们,请使用 "i" 参数(要求拒绝非法语言标签)并捕获 RangeError 异常。
function localeCompareSupportsLocales() {
try {
"foo".localeCompare("bar", "i");
} catch (e) {
return e.name === "RangeError";
}
return false;
}
使用语言环境
localeCompare() 提供的结果因语言而异。为了获得应用程序用户界面所用语言的排序顺序,请确保使用 locales 参数指定该语言(以及可能的备用语言)。
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
使用选项
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
数字排序
// 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
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-string.prototype.localecompare |
| ECMAScript® 2026 国际化 API 规范 # sup-String.prototype.localeCompare |
浏览器兼容性
加载中…