Locale-Aware Sorting
Sort strings correctly with Intl.Collator.
Locale-Aware Sorting is a free JavaScript Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Default Sort Fails
Array sort() compares strings by UTF-16 code units. That puts uppercase before lowercase and mishandles accents — wrong for human-readable lists.
const words = ["banana", "Apple", "cherry"];
console.log([...words].sort()); // ["Apple","banana","cherry"] — capital winslocaleCompare
String.prototype.localeCompare compares two strings using locale rules, returning negative, zero, or positive.
console.log("apple".localeCompare("banana")); // -1
console.log("banana".localeCompare("apple")); // 1Sorting with localeCompare
Pass it as the comparator to get human-friendly ordering.
const words = ["banana", "Apple", "cherry"];
console.log([...words].sort((a, b) => a.localeCompare(b)));
// ["Apple", "banana", "cherry"] — case-insensitive orderingIntl.Collator
For sorting many items, Intl.Collator is faster: build it once and reuse its compare method.
const collator = new Intl.Collator("en-US");
const words = ["banana", "Apple", "cherry"];
console.log([...words].sort(collator.compare));Accents and Diacritics
Collators order accented characters correctly per locale, unlike code-unit comparison.
const words = ["zebra", "ela", "elf", "elan"];
const collator = new Intl.Collator("fr");
console.log([...words].sort(collator.compare));Sensitivity
sensitivity controls what differences matter: "base" ignores case and accents, "accent" ignores case only, "variant" distinguishes all.
const c = new Intl.Collator("en", { sensitivity: "base" });
console.log(c.compare("apple", "APPLE")); // 0 (treated equal)Numeric Sorting
numeric: true sorts embedded numbers by value, so "file2" comes before "file10".
const files = ["file10", "file2", "file1"];
const c = new Intl.Collator("en", { numeric: true });
console.log([...files].sort(c.compare)); // ["file1","file2","file10"]Case-First Ordering
caseFirst: "upper" or "lower" decides whether capitals sort before lowercase when other differences are equal.
const c = new Intl.Collator("en", { caseFirst: "upper" });
console.log(["b", "B", "a", "A"].sort(c.compare));Locale Matters for Order
Different languages alphabetize differently. In Swedish certain accented letters sort after z, not near a.
const words = ["z", "a", "o"];
console.log([...words].sort(new Intl.Collator("sv").compare));Searching with usage
usage: "search" tunes the collator for matching rather than ordering — useful for filtering accent-insensitively.
const c = new Intl.Collator("en", { usage: "search", sensitivity: "base" });
function matches(a, b) { return c.compare(a, b) === 0; }
console.log(matches("cafe", "CAFE")); // trueSorting Objects
Use the collator on a key when sorting arrays of objects.
const people = [{ name: "Bob" }, { name: "alice" }, { name: "Carol" }];
const c = new Intl.Collator("en");
console.log(people.sort((a, b) => c.compare(a.name, b.name)).map((p) => p.name));Quick Check
Test locale-aware sorting.
Recap: Locale-Aware Sorting
You saw why default sort() is wrong, used localeCompare and the faster Intl.Collator, handled accents, sensitivity, numeric and case ordering, locale-specific alphabets, and search matching. That completes the Intl course.
Frequently asked questions
Is the “Locale-Aware Sorting” lesson free?
Yes — the full text of “Locale-Aware Sorting” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.
What will I learn in “Locale-Aware Sorting”?
Sort strings correctly with Intl.Collator. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start JavaScript Academy?
No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Locale-Aware Sorting” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this JavaScript Academy lesson?
Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.