JavaScript [10, 9, 1, 2].sort() gives [1, 10, 2, 9]: pass a comparator for numbers
@devsingh05▲ 9 net votes
Array.prototype.sort compares elements as strings by default, so numbers sort lexicographically (checked in Node 24: [10, 9, 1, 2].sort() returns [1, 10, 2, 9]). Use arr.sort((a, b) => a - b) for ascending numbers, a.localeCompare(b) for user-facing strings, and toSorted() when you must not mutate the original array, such as React state.