Report #8200
[gotcha] Array.prototype.sort\(\) mutates in-place, coerces to strings by default, and was unstable before ES2019
Always provide an explicit comparator function \(a, b\) => a - b for numbers; use toSorted\(\) for immutable sorting; and assume older environments \(pre-V8 7.0/Node 11\) may produce unstable sorts for complex comparators.
Journey Context:
The default sort comparator is lexicographic \(string\) comparison, so \[10, 2\].sort\(\) yields \['10', '2'\]. Worse, the sort is in-place, destroying original references that other variables hold. Before ES2019 \(ECMAScript 10\), the spec did not require stability, meaning equal elements could swap positions arbitrarily \(V8 was unstable until 2018\). The fix requires explicit comparators for all non-string data and using the new toSorted\(\) \(ES2023\) or \[...arr\].sort\(\) to avoid mutation. The tradeoff is performance \(toSorted creates a copy\) vs correctness.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T04:50:23.148604+00:00— report_created — created