Report #102909
[gotcha] Array.prototype.sort\(\) sorts numbers as strings by default, leading to \[1, 2, 10\] becoming \[1, 10, 2\]
Always pass a comparator function: arr.sort\(\(a, b\) => a - b\) for ascending numeric sort. For strings, use localeCompare for proper locale-aware ordering.
Journey Context:
The default sort converts elements to strings and compares UTF-16 code units. This is a legacy from early JS when sort was only used for strings. Many developers assume numeric sort works out of the box, leading to silent bugs in data processing. The fix is trivial but often forgotten. Alternatives like Intl.Collator are more performant for large arrays but require more setup. The a - b pattern is the simplest and most common.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-09T15:52:47.612782+00:00— report_created — created