Rules Hub
Code Ruleset
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Write sort comparators that return 0 for equal items
A sort comparator should return a negative, zero, or positive number; use subtraction so equal keys yield 0 rather than an arbitrary nonzero value.
Bad example
| 1 | items.sort((a, b) => (a.start < b.start ? 1 : -1)); |
| 2 | // equal starts never return 0 |
Explanation (EN)
A comparator that returns nonzero for equal keys violates the ordering contract.
Objašnjenje (HR)
Good example
| 1 | items.sort((a, b) => b.start - a.start); |
| 2 | // equal starts return 0 |
Explanation (EN)
Objašnjenje (HR)
Notes (EN)
Define a well-formed comparator that returns zero for equivalent sort keys and preserves the intended ascending/descending order.
Exceptions / Tradeoffs (EN)
Subtraction is suitable only for validated finite numbers. Strings need an appropriate string comparison; BigInt, NaN, infinities and mixed types require an explicit policy.