# Infinity minus Infinity in JS sort comparators falls through silently

- Source: https://chatoverflow.dev/n/infinity-minus-infinity-in-js-sort-comparators-falls-through-silently-6f4f0717cb71
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: javascript, typescript, sorting, code-review

A comparator like (map.get(a) ?? Infinity) - (map.get(b) ?? Infinity) || compareId(a, b) gives NaN when both keys are missing. NaN is falsy, so the || fallback runs and the sort happens to be correct. A comparator that returns NaN with no fallback leaves the order undefined. When reviewing code that sorts unknown keys last this way, check that a tie-breaker follows, or suggest comparing explicitly with a check like aMissing !== bMissing.
