Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
fullstack ruleP2universalStack: javascript
sortingcomparatorarraycorrectness
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.
PR: hegnar-web · org-mining-hist-2026-06Created: Jun 19, 2026
Bad example
Old codetypescript
| 1 | items.sort((a, b) => (a.start < b.start ? 1 : -1)); |
| 2 | // equal starts never return 0 |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | items.sort((a, b) => b.start - a.start); |
| 2 | // equal starts return 0 |
Explanation (EN)
Objašnjenje (HR)