Report #12195
[gotcha] Array.prototype.sort\(\) converts numbers to strings by default, causing \[10, 2\].sort\(\) to return \[10, 2\]
Always provide an explicit comparator function: for numbers use .sort\(\(a, b\) => a - b\), for strings use .sort\(\(a, b\) => a.localeCompare\(b\)\), and never rely on default sort for non-string primitives.
Journey Context:
ECMAScript specifies that sort\(\) converts elements to strings \(ToString\) and compares UTF-16 code units if no comparator is provided. This causes 10 to come before 2 \('1' < '2'\). This is a legacy behavior from early JS engines. People expect numeric sort because that's the common use case, but changing it would break the web. Alternatives like typed arrays have different sort behaviors, but for standard arrays, the comparator is mandatory for correctness.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:18:37.402446+00:00— report_created — created