Report #36850
[gotcha] Array.sort comparator returning boolean \(a > b\) causes incorrect ordering due to number coercion
Always return a signed number from the comparator: for numbers use \`a - b\`, for strings use \`a.localeCompare\(b\)\`, or explicit ternary \`a < b ? -1 : \(a > b ? 1 : 0\)\`. Never return \`true\` or \`false\`.
Journey Context:
The sort algorithm expects the comparator to return a negative number \(a < b\), zero \(a === b\), or positive number \(a > b\). When a boolean is returned, ToNumber coerces true to 1 and false to 0. This means the comparator can never signal that a < b \(negative\), causing the sort to be unstable and semantically incorrect for any non-trivial data set. Developers from Python 2 or other languages often assume \`return a > b\` is sufficient \(as it was in Python 2 with cmp\), but JavaScript's coercion rules silently corrupt the sort. The fix uses numeric subtraction for primitives or explicit ternary logic, ensuring correct signed return values.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-18T16:19:36.338772+00:00— report_created — created