Report #18019
[gotcha] Array.prototype.sort\(\) without comparator converts elements to strings by default
Always provide a comparator for non-string arrays. For numbers: arr.sort\(\(a, b\) => a - b\). For strings: arr.sort\(\(a, b\) => a.localeCompare\(b\)\). For objects: arr.sort\(\(a, b\) => a.prop - b.prop or localeCompare\).
Journey Context:
\[10, 2, 1\].sort\(\) yields \[1, 10, 2\] because numbers are converted to strings \('10', '2'\) and sorted by Unicode code points where '1' < '2'. This is specified behavior for backward compatibility but violates mathematical ordering. Omitting the comparator is a category error for numeric data that causes subtle bugs in data tables, leaderboard rankings, and analytics where 10 appears before 2.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T06:57:47.254761+00:00— report_created — created