Why JavaScript sort sorts wrong — 10 lands before 9, plus 2 more causes

You sort prices from lowest to highest and get this:
[10, 9, 1, 100].sort()
// [1, 10, 100, 9]
A hundred comes before nine. The code isn't broken — it does exactly what the standard says. It's just that by default sort compares text, not numbers. Here are three reasons sorting comes out crooked, starting with the most common.
Cause 1. Numbers are compared as text
If you don't tell sort how to compare, it turns every item into a string and arranges them like words in a dictionary. First character, then the second.
In a dictionary "10" comes before "9", because the character "1" is less than "9". Just like "apricot" comes before "banana" even though it's longer. Length doesn't matter — the first character does.
How to check. Look at the call. If the brackets are empty — arr.sort() — and the array holds numbers, you've found it. Numbers of different lengths give it away: 9 and 10, 99 and 100.
How to fix. Pass sort a compare function:
prices.sort((a, b) => a - b) // ascending: [1, 9, 10, 100]
prices.sort((a, b) => b - a) // descending: [100, 10, 9, 1]
That little function is a callback. sort calls it for pairs of items and looks at the sign of the answer. Below zero — a goes first. Above zero — b goes first. Zero — order doesn't matter.
Objects are sorted by a field the same way: products.sort((a, b) => a.price - b.price).
If prices arrived as strings like "$1,200", the subtraction gives NaN and sorting goes wrong again. First turn the string into a number, then sort.
Cause 2. The compare function returns true or false
It looks logical, and AI sometimes writes it:
arr.sort((a, b) => a > b) // ❌
The problem is that sort expects a number: negative, zero or positive. Here it gets true or false. Those become 1 and 0 — never negative. To sort that sounds like "a is never less than b".
The result depends on the browser and the array length. In Chrome such an array often stays not sorted at all. Tested: [5, 1, 4, 2, 3].sort((a, b) => a > b) returns [5, 1, 4, 2, 3]. On a tiny test example it may "work" by accident — and then break on real data.
How to check. Look for > or < in the compare function with no numbers around. If the arrow returns a comparison instead of a difference, that's it.
How to fix. For numbers — a - b. For strings — localeCompare, more on it below. If you really want >, spell it out: a > b ? 1 : a < b ? -1 : 0.
Cause 3. Accented letters and capitals
You sort a list of names or products and see something odd:
["zebra", "Émile", "apple", "Banana", "éclair"].sort()
// ["Banana", "apple", "zebra", "Émile", "éclair"]
The accented words drifted to the very end, after "zebra", and "Banana" jumped ahead of "apple". That's because sort without a function compares character codes. Accented letters like "é" have codes greater than every plain Latin letter. And capitals come before lowercase: "Z" lands before "a". Russian has the same trap with the letter "ё", which ends up after "я".
How to check. The list has accented letters, or words with upper- and lowercase mixed — and the order doesn't look alphabetical.
How to fix. Compare by the rules of the language:
names.sort((a, b) => a.localeCompare(b))
// ["apple", "Banana", "éclair", "Émile", "zebra"]
localeCompare knows the alphabet: "é" sits next to "e", and case doesn't wreck the order. For Russian, pass the language: a.localeCompare(b, "ru").
Bonus: sort mutates the original array
Even a correct sort can bring a surprise. sort rearranges items in the array itself and returns that same array. If the array is used anywhere else, the order changes there too. In React this is why the screen doesn't update.
If you need a sorted version but mustn't touch the original, use toSorted: const sorted = prices.toSorted((a, b) => a - b). It's the same distinction as between slice and splice — more in slice vs splice.
How do I sort an array in descending order?
Swap a and b: (a, b) => b - a. For strings — (a, b) => b.localeCompare(a).
Why do dates sort wrong?
If dates are strings like "09/27/2026", sort compares them as text, starting from the month. Turn them into dates and subtract: (a, b) => new Date(a.iso) - new Date(b.iso). Strings in "2026-09-27" format already sort correctly as text.
Can I sort an array of objects by name?
Yes: items.sort((a, b) => a.title.localeCompare(b.title)). It's the most common case for catalogs and lists.
The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.





