Collator Sensitivity Ladder Compare Rules
Intl.Collator sensitivity climbs from base to variant: base folds case and accents, accent keeps accents, case keeps case, and variant distinguishes both.
Code
const ladder = ['base', 'accent', 'case', 'variant'];
for (const s of ladder) {
const c = new Intl.Collator('en', { sensitivity: s });
console.log(
s.padEnd(8),
'e~E:', c.compare('e', 'E') === 0 ? 'eq' : 'diff',
'| e~é:', c.compare('e', 'é') === 0 ? 'eq' : 'diff',
);
}
// ignorePunctuation 让 a-b 与 a b 相等:标点不参与比较
const loose = new Intl.Collator('en', { ignorePunctuation: true, sensitivity: 'variant' });
console.log('punct ignored:', loose.compare('a-b', 'a b') === 0);
const strict = new Intl.Collator('en', { sensitivity: 'variant' });
console.log('punct counted:', strict.compare('a-b', 'a b') === 0);Output
Captured from a sandboxed run of this snippet in the build pipeline.
base e~E: eq | e~é: eq
accent e~E: eq | e~é: diff
case e~E: diff | e~é: eq
variant e~E: diff | e~é: diff
punct ignored: true
punct counted: falseThe mechanics in play
A single loop drives the file — for (const s of ladder).
Demo state sits in top-level bindings: ladder, loose, strict.
What to watch for
- No package arrives from outside — built-in APIs of the language back everything.
Using it
The demo output is labeled (punct ignored:, punct counted:); each label names the call that printed it.
Key statements
const loose = new Intl.Collator('en', { ignorePunctuation: true, sensitivity: 'variant' });
const strict = new Intl.Collator('en', { sensitivity: 'variant' });
const c = new Intl.Collator('en', { sensitivity: s });
Interactive Tools for This Topic
FAQ
- Anything to download besides the language toolchain?
- Nothing beyond the toolchain you already have for this language. Its state lives in module-level bindings: `ladder`, `loose`, `strict`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.