String.prototype.localeCompare()

Baseline 已廣泛支援

此特性已相當成熟,可在許多裝置和瀏覽器版本上使用。自 ⁨2015 年 7 月⁩以來,各瀏覽器均已提供此特性。

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

語法

js
localeCompare(compareString)
localeCompare(compareString, locales)
localeCompare(compareString, locales, options)

引數

localesoptions 引數自定義函式行為,並允許應用程式指定應使用的語言的格式約定。

在支援 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 的實現中,此引數將被忽略。

有關 localesoptions 引數的詳細資訊以及如何使用它們,請參閱 Intl.Collator() 建構函式

返回值

如果 referenceStr 出現在 compareString 之前,則返回一個負數;如果 referenceStr 出現在 compareString 之後,則返回正數;如果它們相等,則返回 0

在支援 Intl.Collator 的實現中,這等同於 new Intl.Collator(locales, options).compare(referenceStr, compareString)

描述

返回一個整數,指示 referenceStr 出現在 compareString 之前、之後還是與之等效。

  • referenceStr 出現在 compareString 之前時為負數
  • referenceStr 出現在 compareString 之後時為正數
  • 相等時返回 0

警告:不要依賴於 -11 的精確返回值!

負數和正數整數結果在不同瀏覽器(以及不同瀏覽器版本)之間會有所不同,因為 ECMAScript 規範僅強制要求負數和正數值。某些瀏覽器可能會返回 -22,甚至其他負數或正數值。

示例

使用 localeCompare()

js
// 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() 可以實現陣列的忽略大小寫排序。

js
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é']

檢查瀏覽器對擴充套件引數的支援

並非所有瀏覽器都支援 localesoptions 引數。

要檢查實現是否支援它們,請使用 "i" 引數(要求拒絕非法語言標籤)並捕獲 RangeError 異常。

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

使用語言環境

localeCompare() 提供的結果因語言而異。為了獲得應用程式使用者介面所用語言的排序順序,請確保使用 locales 引數指定該語言(以及可能的備用語言)。

js
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 引數進行自定義。

js
// 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

數字排序

js
// 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

瀏覽器相容性

另見