CKTIS / JavaScript
Intl.Collator: sensitivity and locale rules
Compare and sort strings using the selected locale’s rules. The `sensitivity` option controls which differences matter to the comparison.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
new Intl.Collator(locales, { sensitivity: "base" | "accent" | "case" | "variant", usage: "sort" | "search" })
collator.compare(a, b)
strings.sort(collator.compare)
Intl.Collator.supportedLocalesOf(locales)
collator.resolvedOptions()
The `|` notation lists alternatives; pass one string value per option. Both constructor arguments and both shown options are optional.With Intl.Collator, the effective locale’s collation rules determine what counts as a base letter. Under default German collation, ä is a variant of a; under Swedish collation, it is a separate letter. That is why "base" can treat the two strings as equivalent in de but not in sv.
Different base letters are distinguished at every sensitivity setting. For variants of the same base letter, "base" ignores diacritics and case; "accent" distinguishes diacritics but ignores case; "case" does the reverse; and "variant" distinguishes both and may distinguish other differences too. A value outside this list, such as "ignore", throws a RangeError.
With the default usage: "sort", sensitivity defaults to "variant". With usage: "search", the sensitivity default depends on the locale; in that mode, test for a match with compare(a, b) === 0, not for ordering. When sorting, a negative result means a comes before b, a positive result means it comes after, and zero means the comparison treats them as equivalent. Do not expect exactly -1 or 1. The collator.compare function is bound to its instance, so you can pass it directly to sort(). A result of 0 does not imply a === b.
Request a locale explicitly: if none of the requested locales can be matched, the runtime uses its default. Intl.Collator.supportedLocalesOf() checks support without falling back to the runtime default, and resolvedOptions().locale shows the effective locale. Collation data can vary across runtimes and versions, even with the same resolved options.
Examples
{
const locales = ["en", "de", "sv"];
if (typeof Intl === "undefined" || typeof Intl.Collator !== "function" ||
Intl.Collator.supportedLocalesOf(locales).length !== locales.length) {
console.log("Intl.Collator or required locales unavailable");
} else {
for (const sensitivity of ["base", "accent", "case", "variant"]) {
const { compare } = new Intl.Collator("en", { sensitivity });
console.log(`${sensitivity}: ${compare("a", "á") === 0}, ${compare("a", "A") === 0}`);
}
for (const locale of ["de", "sv"]) {
const { compare } = new Intl.Collator(locale, { sensitivity: "base" });
console.log(`${locale}: ${compare("a", "ä") === 0}`);
}
}
}Result
base: true, true accent: false, true case: true, false variant: false, false de: true sv: false
In the first four lines, `true` means the pair compares as equivalent: first `a` and `á`, then `a` and `A`. The last two lines compare `a` with `ä` at `"base"` in `de` and `sv`. The shown output assumes support for these locales and `en`, with the documented default collation rules.
Related lessons
- JavaScript · Lesson 24 · Intermediate · Version: 3