On this page
Intl.Collator
Prev: DateTimeFormat | Up: Intl | Next: Global
Rel: Intl, String, RegExp & Intl Guide
Ref: MDN Web Docs — Intl.Collator
Tags: #public #web #javascript #builtins #intl #tier/standard
Signature & Overview
new Intl.Collator(locales?, options?)
Locale-aware string comparison (including natural numeric sorting with { numeric: true }).
Notes & Pitfalls
- Reuse one
Intl.Collatorinstance instead of callingstr.localeCompare()repeatedly insidesort()—much faster for large arrays.
Example
const collator = new Intl.Collator("en", { numeric: true, sensitivity: "base" });
const files = ["img10.png", "img2.png", "img1.png"].toSorted(collator.compare);
console.assert(JSON.stringify(files) === '["img1.png","img2.png","img10.png"]');
Linked from
Related notes
- tuts/javascript/builtins/Intl/DateTimeFormat Linked here; links to this page; shared tags: #builtins, #intl, #javascript, #tier, #web
- tuts/javascript/builtins/Intl Linked here; links to this page; shared tags: #builtins, #intl, #javascript, #web
- tuts/javascript/builtins/Global Linked here; shared tags: #builtins, #javascript, #web
- tuts/javascript/builtins/string_regexp_and_intl Linked here; shared tags: #builtins, #javascript, #web
- tuts/javascript/builtins/Intl/NumberFormat shared tags: #builtins, #intl, #javascript, #tier, #web
- tuts/javascript/builtins/Array/at shared tags: #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/filter shared tags: #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/find shared tags: #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/flatMap shared tags: #builtins, #javascript, #tier, #web
- tuts/javascript/builtins/Array/from shared tags: #builtins, #javascript, #tier, #web