Report #11276
[gotcha] Array.sort\(\) produces inconsistent ordering when comparator returns boolean
Always return a signed number: negative if a < b, zero if equal, positive if a > b. For numbers use \`a - b\`. For strings use \`a.localeCompare\(b\)\`. Never return \`a > b\` or boolean literals.
Journey Context:
ECMAScript's sort algorithm \(stable since ES2019\) requires the comparator to return a Number indicating relative sort order. Returning a boolean coerces to 1 or 0, losing the negative \(less-than\) signal. Engines may interpret boolean results inconsistently, producing partial or random-looking orders. Developers often write \`\(a, b\) => a.id > b.id\` expecting ascending sort, but this actually checks 'is a greater?' and returns true/false, which sort interprets incorrectly. The numeric subtraction pattern is required for correct primitive sorting.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:53:18.972048+00:00— report_created — created