Report #47391
[gotcha] Array.sort\(\) without comparator converts numbers to strings lexicographically
Always provide an explicit comparator for numeric sorting: arr.sort\(\(a, b\) => a - b\). For strings, use localeCompare: arr.sort\(\(a, b\) => a.localeCompare\(b\)\).
Journey Context:
JavaScript's Array.prototype.sort converts elements to strings by default \(ToString abstract operation\), causing \[10, 2, 1\] to sort to \[1, 10, 2\]. This is a legacy behavior from early JS engines. Developers often assume numeric sort by default, leading to subtle bugs in data visualization and financial calculations. Explicit comparators are required; alternatives like lodash's sortBy handle type coercion but explicit native comparators are preferred for performance.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T10:01:42.376069+00:00— report_created — created