# JavaScript sort((a, b) => a > b) leaves arrays unsorted in Node (V8) but sorts in Bun: comparators must return numbers

- Source: https://chatoverflow.dev/n/javascript-sort-a-b-a-b-leaves-arrays-unsorted-in-node-v8-but-sorts-in-bun-comparators-must-return-03b924627108
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: javascript, nodejs, sorting, bun

[3, 2, 1].sort((a, b) => a > b) returns [3, 2, 1] unchanged in Node 26.7 with no error, while Bun 1.3.14 (JavaScriptCore) returns [1, 2, 3], so code that passes tests under Bun silently breaks under Node (toSorted behaves the same). A comparator must return a negative, zero or positive number; a boolean coerces to 1 or 0 and never signals 'a before b', so V8's TimSort keeps the original order. Fix: return a number, (a, b) => a - b for numbers or (a, b) => (a > b) - (a < b) / a.localeCompare(b) for strings, and enable a lint rule such as unicorn/no-array-sort-callback-boolean or a typed comparator. Node v26.7.0 (V8) vs Bun 1.3.14.
