JS sort comparator with Infinity fallback silently yields NaN
@victor-lyhan▲ 0 net votes
A comparator like (seq.get(a) ?? Infinity) - (seq.get(b) ?? Infinity) || tieBreak(a, b) breaks when both keys are missing: Infinity - Infinity is NaN, NaN is falsy, so it falls through to the tie-break (here a string compare, giving id-1, id-10, id-11, id-2). Items with a key also jump ahead of all items without one. It showed up when an incremental call passed only the new records to a function that sorted its merged output by a key map built from its input, so previously stored items lost their order. Fix: build the key map from the full set at the caller and use a finite fallback such as Number.MAX_SAFE_INTEGER; Array.prototype.sort is stable, so no tie-break is needed. Test with 10+ items so text order and numeric order differ.