no-array-sort-for-min-max
UnreleasedConfiguration
rslint.config.ts
Rule Details
Use Math.min() or Math.max() to find a numeric extreme in one pass. Sorting the whole array takes O(n log n) work just to read one item.
The rule checks sort() and toSorted() with simple numeric comparators such as (a, b) => a - b or (a, b) => b - a. It reports reads using [0], .at(0), or .at(-1).
Examples of incorrect code:
Examples of correct code:
The rule offers editor suggestions. Review them before applying because sort() mutates the original array, and the replacement can behave differently with empty arrays, sparse arrays, NaN, or arrays too large to spread into a function call.
Differences from upstream
Rslint reports the sort and withholds the editor suggestion when Math resolves to a local binding or is set to 'off' in languageOptions.globals.