Report #101531
[gotcha] Array.prototype.sort\(\) without a comparator coerces elements to strings, breaking numeric order
Always pass an explicit comparator: nums.sort\(\(a, b\) => a - b\). For mixed types, define a comparator function that matches your domain ordering.
Journey Context:
The default sort converts every element to a string and compares UTF-16 code units. \[9, 10, 2\].sort\(\) becomes \['10', '2', '9'\]. This is a legacy behavior from early JS and is one of the most common interview bugs. Passing a comparator is cheap insurance and documents intent.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-07T05:00:49.256862+00:00— report_created — created