Report #5283
[gotcha] Array.prototype.sort\(\) mutates in-place and converts elements to strings by default, breaking numeric sorting
Always provide an explicit comparator: \`arr.sort\(\(a, b\) => a - b\)\` for numbers \(handle NaN separately\), or \`\(a, b\) => a.localeCompare\(b\)\` for strings. To avoid mutation, spread first: \`\[...arr\].sort\(...\)\`.
Journey Context:
\`sort\` is not a pure function; it rearranges the array in-place and returns the same reference \(\`arr.sort\(\) === arr\`\), violating immutable patterns. Without a compare function, elements are converted to strings and sorted by UTF-16 code units, causing \`11\` to sort before \`2\` \(lexicographically\). The comparator must return negative, zero, or positive numbers; returning \`true/false\` causes incorrect sorting. NaN comparisons always return false, so comparators must explicitly handle NaN to avoid unstable sort behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:57:42.033950+00:00— report_created — created