Skip to main content

Command Palette

Search for a command to run...

Mastering localeCompare() in JavaScript: The Key to Human-Friendly String Sorting

Published
3 min read

✍️ Introduction

When we sort strings in JavaScript, we often reach for the trusty .sort() method. But here’s the catch:
.sort() compares strings using Unicode code points, not how humans think about alphabetical order.

That’s why you might see strange results like this:

["Zebra", "apple", "Banana"].sort();
// → ["Banana", "Zebra", "apple"]

Why is Banana before apple? Because uppercase letters have lower Unicode values than lowercase letters.

👉 This is where localeCompare() comes in.

It helps JavaScript compare strings in a way that respects language rules, accents, and even numbers.


🟢 What is localeCompare()?

localeCompare() is a string method that compares two strings and returns:

  • -1 → if the first string comes before the second

  • 0 → if they are equal

  • 1 → if the first string comes after the second

✅ Example:

console.log("apple".localeCompare("banana")); // -1
console.log("banana".localeCompare("apple")); // 1
console.log("apple".localeCompare("apple"));  // 0

🟢 Why not just use .sort()?

JavaScript’s default .sort() works fine for simple cases, but:

  • Uppercase comes before lowercase.

  • Accented letters like é or ä don’t sort naturally.

  • Numbers sort lexicographically: "10" comes before "2".

Example:

["10", "2", "1"].sort();
// → ["1", "10", "2"]

Humans expect 1, 2, 10 — not 1, 10, 2.


🟢 How to Use localeCompare() for Sorting

Here’s how to integrate it with .sort():

let fruits = ["Zebra", "apple", "Banana"];
fruits.sort((a, b) => a.localeCompare(b));

console.log(fruits);
// → ["apple", "Banana", "Zebra"]

🎉 Now the list is sorted the way humans expect.


🟢 Adding Locales

localeCompare() takes an optional locale parameter, so sorting rules adapt to languages:

console.log("ä".localeCompare("z", "sv")); // -1 (Swedish: ä comes before z)
console.log("ä".localeCompare("z", "en")); //  1 (English: ä comes after z)

This is essential for international apps.


🟢 Useful Options

You can pass an options object for more control:

1️⃣ Case-insensitive sorting

["apple", "Apple"].sort((a, b) => a.localeCompare(b, "en", { sensitivity: "base" }));
// → ["apple", "Apple"]

2️⃣ Numeric sorting

["10", "2", "1"].sort((a, b) => a.localeCompare(b, "en", { numeric: true }));
// → ["1", "2", "10"]

✅ Now "10" is treated as 10, not as "1" followed by "0".


🟢 Full Example

let items = ["Zebra", "apple", "Banana", "ábc", "2alpha", "10alpha"];

let sorted = items.sort((a, b) =>
  a.localeCompare(b, "en", { numeric: true, sensitivity: "base" })
);

console.log(sorted);
// → ["2alpha", "10alpha", "abc", "apple", "Banana", "Zebra"]
  • numeric: true → sorts numbers like humans expect.

  • sensitivity: "base" → ignores case and accent differences for simpler sorting.


🎯 When Should You Use localeCompare()?

✅ When building search or filter features.
✅ When sorting user-facing lists (names, cities, countries).
✅ When handling multiple languages or accented characters.
✅ When sorting mixed strings with numbers (product codes, filenames).


✍️ Conclusion

JavaScript’s .sort() is fine for quick scripts, but for real-world applications, you’ll need more power.
That’s why localeCompare() is a must-know method for every developer.

It’s the bridge between machine order and human order.