Report #62370
[gotcha] Array.prototype.sort\(\) coerces numbers to strings by default, breaking numeric sorting
Always provide an explicit comparator function for numeric or date sorting: arr.sort\(\(a, b\) => a - b\). Never rely on default sort for anything other than lexicographic string sorting.
Journey Context:
The ECMAScript spec mandates that sort converts elements to strings \(UTF-16 code units\) if no comparator is provided. This causes \[10, 2, 1\].sort\(\) to yield \[1, 10, 2\]. Developers often assume 'smart' numeric sorting because other languages behave differently or because small test arrays like \[1,2,3\] happen to work. The result is silent data corruption in sorted UI lists, financial reports, or game leaderboards where '10' is lexically less than '2'.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T11:10:21.287404+00:00— report_created — created