Skip to content

sort-comparator ​

A comparator that doesn't return a number, or .sort() on numbers without one.

Why: a comparator must return a negative number, zero or a positive number. One that returns a > b is never negative, so the result depends on the engine's algorithm (v8.dev). And without a comparator, .sort() compares strings: [10, 9, 1].sort() is [1, 10, 9].

✕ Rejectedsort-comparator
ts
export function ranked(scores: readonly number[]): number[] {
  const copy: number[] = [...scores];
  copy.sort();
  return copy;
}
✓ Accepted0 diagnostics
ts
export function ranked(scores: readonly number[]): number[] {
  const copy: number[] = [...scores];
  copy.sort((a, b) => a - b);
  return copy;
}

A comparator that returns a boolean ((a, b) => a < b) is reported too. In typed code TypeScript already rejects it; the rule catches it in JavaScript files and untyped callbacks.

Quick fix: Sort numerically: (a, b) => a - b for arrays known to hold numbers (a number[] annotation or a literal of numbers).

What the rule can't see: arrays whose element type isn't visible where they are made; a comparator that returns NaN or inconsistent results.

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra