--- title: Intl.Collator slug: Web/JavaScript/Reference/Global_Objects/Intl/Collator tags: - Class - Collator - Internationalization - Intl - JavaScript - Localization - Reference translation_of: Web/JavaScript/Reference/Global_Objects/Intl/Collator ---
Intl.Collator
オブジェクトは、言語を考慮した文字列の比較を可能にします。
Collator
オブジェクトを生成します。次の例では、一方の文字列が他方の文字列の前である場合、後である場合、および同じレベルである場合の比較結果を表示します。
console.log(new Intl.Collator().compare('a', 'c')); // → 負の値 console.log(new Intl.Collator().compare('c', 'a')); // → 正の値 console.log(new Intl.Collator().compare('a', 'a')); // → 0
上記コードの結果は、ブラウザーやブラウザーのバージョンによって異なる可能性がある点に注意してください。というのも前および後を表す数値については、それぞれ負の数および正の数であることだけが仕様によって定めています。具体的な数値は定められていないため、実装ごとに異なる数値になるかもしれません。
{{jsxref("Intl/Collator/compare")}} の結果は言語によって異なります。アプリケーションのユーザーインターフェイスで使用される言語のソート順を取得するには、 locales
引数にその言語を (およびフォールバック用の言語も) 指定してください。
// in German, ä sorts with a console.log(new Intl.Collator('de').compare('ä', 'z')); // → 負の値 // in Swedish, ä sorts after z console.log(new Intl.Collator('sv').compare('ä', 'z')); // → 正の値
T{{jsxref("Intl/Collator/compare")}} の結果は options
引数でカスタマイズできます。
// in German, ä has a as the base letter console.log(new Intl.Collator('de', { sensitivity: 'base' }).compare('ä', 'a')); // → 0 // in Swedish, ä and a are separate base letters console.log(new Intl.Collator('sv', { sensitivity: 'base' }).compare('ä', 'a')); // → 正の値
仕様書 |
---|
{{SpecName('ES Int Draft', '#collator-objects', 'Intl.Collator')}} |