Intl.Collator.prototype.compare()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2017年9月.
Intl.Collator.prototype.compare() メソッドは、 2 つの文字列をこの Intl.Collator オブジェクトのソート順に従って比較します。
試してみましょう
構文
引数
string1,string2-
互いに比較する文字列です。
解説
compare ゲッター関数は、 string1 と string2 をこの Intl.Collator オブジェクトのソート順に従って比較した結果を数値で返します。 string1 が string2 の前にくる場合は負の値、 string1 が string2 の後にくる場合は正の値、等しいとみなされる場合は 0 を返します。
例
配列の並べ替えにおける compare の使用
配列の並べ替えのために compare ゲッター関数を使用します。なお、この関数は、取得元の collator にバインドされているので、直接 Array.prototype.sort() に渡すことができます。
配列の検索における compare の使用
配列内の文字列の検索のために compare ゲッター関数を使用します。
仕様書
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification # sec-intl.collator.prototype.compare |