Report #9527
[gotcha] Array.prototype.sort converts elements to strings by default causing numeric sort failures
Always provide a comparator for numeric sorting: arr.sort\(\(a, b\) => a - b\); for strings, use localeCompare: arr.sort\(\(a, b\) => a.localeCompare\(b\)\)
Journey Context:
Developers expect \[10, 2\].sort\(\) to yield \[2, 10\], but ECMAScript specifies that sort converts elements to strings \(via toString\) and compares UTF-16 code units. This places '10' before '2' \(since '1' < '2'\), causing subtle bugs in data visualization and ranking algorithms. While Python and Ruby have separate sort/sort\_by, JavaScript overloads sort\(\) with an optional comparator, making the string coercion default surprising.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T08:22:32.202512+00:00— report_created — created