Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[JavaScript] comparing order with Intl.Collator("ja-JP")
const col = new Intl.Collator("ja-JP");
console.log("[category]");
// Category order: symbol < number < kana < kanji < Greek < Cyrillic
console.log(col.compare("-", "←")); //=> -1 (A < B)
console.log(col.compare("←", "♪")); //=> -1 (A < B)
console.log(col.compare("♪", "0")); //=> -1 (A < B)
console.log(col.compare("9", "A")); //=> -1 (A < B)
console.log(col.compare("Z", "あ")); //=> -1 (A < B)
console.log(col.compare("ん", "亜")); //=> -1 (A < B)
console.log(col.compare("鯺", "α")); //=> -1 (A < B)
console.log(col.compare("ω", "Д")); //=> -1 (A < B)
console.log("[symbol]");
// Symbol
console.log(col.compare("{", "}")); //=> -1 (A < B)
console.log(col.compare("}", "}")); //=> 0 (A = B)
console.log(col.compare("↑", "⇦")); //=> -1 (A < B) arrow style is not variant or deco
console.log("[number]");
// Number
console.log(col.compare("0", "0")); //=> 0 (A = B)
console.log(col.compare("〇", "0")); //=> 0 (A = B) JIS Kanji-zero in symbols
console.log(col.compare("0", "1")); //=> -1 (A < B)
// Number decorated
console.log(col.compare("1", "①")); //=> -1 (A = B)
console.log(col.compare("①", "2")); //=> -1 (A = B)
console.log("[kana]");
// Kana variations are same
console.log(col.compare("ア", "あ")); //=> 0 (A = B)
console.log(col.compare("ア", "ア")); //=> 0 (A = B)
// composed kana symbol
console.log("[composed kana]");
console.log("㌔".normalize("NFKD") === "キロ"); //=> true
console.log(col.compare("キロ", "㌔")); //=> -1 (A < B)
console.log(col.compare("㌔", "キワ")); //=> -1 (A < B)
// Kana order
console.log(col.compare("ア", "イ")); //=> -1 (A < B)
console.log(col.compare("ヲ", "ン")); //=> -1 (A < B)
// Kana accents order: は<ば<ぱ<ひ
console.log(col.compare("は", "ば")); //=> -1 (A < B)
console.log(col.compare("ば", "ぱ")); //=> -1 (A < B)
console.log(col.compare("ぱ", "ひ")); //=> -1 (A < B)
// Smaller kana order: ウ<ェ<エ, チ<ッ<ツ, モ<ャ<ヤ<ュ
console.log(col.compare("ウ", "ェ")); //=> -1 (A < B)
console.log(col.compare("ェ", "エ")); //=> -1 (A < B)
console.log(col.compare("チ", "ッ")); //=> -1 (A < B)
console.log(col.compare("ッ", "ツ")); //=> -1 (A < B)
console.log(col.compare("モ", "ャ")); //=> -1 (A < B)
console.log(col.compare("ャ", "ヤ")); //=> -1 (A < B)
console.log(col.compare("ヤ", "ュ")); //=> -1 (A < B)
// Smaller kana order (added JISX0213 but katakana is in JISX0208):
console.log(col.compare("お", "ゕ")); //=> -1 (A < B)
console.log(col.compare("ゕ", "か")); //=> -1 (A < B)
// Smaller kana order (added JISX0213): リ<ㇼ<ロ, ク<ㇰ<ケ
console.log(col.compare("リ", "ㇼ")); //=> -1 (A < B)
console.log(col.compare("ㇼ", "ル")); //=> -1 (A < B)
console.log(col.compare("ク", "ㇰ")); //=> -1 (A < B)
console.log(col.compare("ㇰ", "ケ")); //=> -1 (A < B)
// Kanji
console.log("[Kanji]");
console.log(col.compare("亜", "阿")); //=> -1 (A < B) JIS level 1 order
console.log(col.compare("枠", "弌")); //=> -1 (A < B) JIS1 <JIS2
console.log(col.compare("弌", "煕")); //=> -1 (A < B) JIS2 order
console.log(col.compare("煕", "俱")); //=> -1 (A < B) JIS2 < others (include JISX0213)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment