Report #58109
[gotcha] Array.sort\(\) mutates in-place and coerces numbers to strings by default
Always use an explicit comparator for non-string arrays: const sorted = arr.slice\(\).sort\(\(a, b\) => a - b\). Never rely on default lexicographic sort for numeric data.
Journey Context:
JavaScript's sort is intentionally polymorphic but carries legacy behavior from the 1990s: it converts elements to strings and compares UTF-16 code units unless a comparator is provided. This causes \[10, 2\].sort\(\) to yield \[10, 2\] rather than \[2, 10\]. Additionally, unlike map/filter, sort mutates the original array reference, violating functional programming expectations and causing side effects in reactive frameworks that detect changes by reference.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T04:01:41.201679+00:00— report_created — created