Skip to content

noArraySortForMinMax (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"noArraySortForMinMax": "error"
}
}
}
}

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 undefined when sorted, but Infinity or -Infinity with Math.min() or Math.max();
  • sort() changes the original array, and the replacement does not;
  • arrays containing NaN or empty slots can give different results;
  • spreading a very large array into a function call can throw an error.
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];