JavaScript Academy · Lesson

Locale-Aware Sorting

Sort strings correctly with Intl.Collator.

Lesson 4 of 413 steps

Locale-Aware Sorting is a free JavaScript Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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 wins

localeCompare

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")); // 1

Sorting 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 ordering

Intl.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")); // true

Sorting 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.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Locale-Aware Sorting” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Locale-Aware Sorting”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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.

All lessons in this course

  1. Formatting Numbers and Currency
  2. Formatting Dates and Times
  3. Relative Time and Plurals
  4. Locale-Aware Sorting
← Back to JavaScript Academy