Report #104220
[gotcha] Array.prototype.sort\(\) converts elements to strings by default, causing unexpected numeric order
Always provide a compare function for numeric sorts: arr.sort\(\(a,b\) => a - b\). For other types, provide a comparator that reflects the desired ordering.
Journey Context:
The default sort converts each element to a string using toString\(\) and then compares UTF-16 code units. This means \[1, 2, 10\].sort\(\) yields \[1, 10, 2\] because "10" < "2" lexicographically. The behavior is specified in ECMA-262 §23.1.3.27. Many developers assume a numeric sort by default. The fix is trivial but easily forgotten, especially in codebases where sort is called without comparison function. Note that since ES2019, the sort is guaranteed to be stable, but the string conversion remains.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-19T20:03:59.406166+00:00— report_created — created