noArraySortForMinMax (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/noArraySortForMinMax - This rule has an unsafe fix.
- The default severity of this rule is warning.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noArraySortForMinMax": "error" } } }}Description
Section titled “Description”Disallow sorting an array only to read its smallest or largest value.
Sorting puts every item of an array in order, which takes far more work than finding
the smallest or largest value. Math.min() and Math.max() look at each item only once.
In addition, sort() changes the order of the original array, which is often unintended.
This rule reports reading the first item ([0] or .at(0)) or the last item (.at(-1))
of an array that was just sorted with sort() or toSorted(), when the comparison
function only subtracts one number from the other, such as (a, b) => a - b.
Sorting without a comparison function, or with any other comparison, is not reported.
The suggested fix replaces the expression with Math.min(...array) or Math.max(...array).
The fix is unsafe because the result can differ:
- an empty array gives
undefinedwhen sorted, butInfinityor-InfinitywithMath.min()orMath.max(); sort()changes the original array, and the replacement does not;- arrays containing
NaNor empty slots can give different results; - spreading a very large array into a function call can throw an error.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const minimum = array.sort((a, b) => a - b)[0];code-block.js:1:17 lint/nursery/noArraySortForMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This sorts the whole array only to read its smallest value.
> 1 │ const minimum = array.sort((a, b) => a - b)[0];
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Sorting does much more work than finding the smallest value, which only needs to look at each item once.
ℹ Use Math.min() with the array spread into its arguments instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.min() instead.
1 │ - const·minimum·=·array.sort((a,·b)·=>·a·-·b)[0];
1 │ + const·minimum·=·Math.min(...array);
2 2 │
const minimum = array.toSorted((a, b) => a - b).at(0);code-block.js:1:17 lint/nursery/noArraySortForMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This sorts the whole array only to read its smallest value.
> 1 │ const minimum = array.toSorted((a, b) => a - b).at(0);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Sorting does much more work than finding the smallest value, which only needs to look at each item once.
ℹ Use Math.min() with the array spread into its arguments instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.min() instead.
1 │ - const·minimum·=·array.toSorted((a,·b)·=>·a·-·b).at(0);
1 │ + const·minimum·=·Math.min(...array);
2 2 │
const maximum = array.sort((a, b) => a - b).at(-1);code-block.js:1:17 lint/nursery/noArraySortForMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This sorts the whole array only to read its largest value.
> 1 │ const maximum = array.sort((a, b) => a - b).at(-1);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Sorting does much more work than finding the largest value, which only needs to look at each item once.
ℹ Use Math.max() with the array spread into its arguments instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.max() instead.
1 │ - const·maximum·=·array.sort((a,·b)·=>·a·-·b).at(-1);
1 │ + const·maximum·=·Math.max(...array);
2 2 │
const maximum = array.toSorted((a, b) => b - a)[0];code-block.js:1:17 lint/nursery/noArraySortForMinMax FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This sorts the whole array only to read its largest value.
> 1 │ const maximum = array.toSorted((a, b) => b - a)[0];
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Sorting does much more work than finding the largest value, which only needs to look at each item once.
ℹ Use Math.max() with the array spread into its arguments instead.
ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
ℹ Unsafe fix: Use Math.max() instead.
1 │ - const·maximum·=·array.toSorted((a,·b)·=>·b·-·a)[0];
1 │ + const·maximum·=·Math.max(...array);
2 2 │
const minimum = Math.min(...array);const maximum = Math.max(...array);const sorted = array.toSorted((a, b) => a - b);const first = array.toSorted((a, b) => a.value - b.value)[0];const second = array.toSorted((a, b) => a - b)[1];Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.