Report #75840
[gotcha] Array.prototype.sort\(\) converts numbers to strings by default, causing 10 to sort before 2
Always provide a comparator function: sort\(\(a, b\) => a - b\) for numbers, or use Intl.Collator for strings
Journey Context:
The spec requires sort\(\) to convert elements to strings using toString\(\) if no comparator is provided, then sort lexicographically by UTF-16 code units. This causes \[1, 11, 2\].sort\(\) to yield \[1, 11, 2\], which surprises developers expecting numeric order. The comparator \(a, b\) => a - b works for numbers but risks overflow for very large integers \(safe integer range issues\). For mixed types or international strings, Intl.Collator is safer. The footgun is assuming 'I'll just sort this array of numbers' without the comparator.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T09:53:41.121226+00:00— report_created — created