Skip to content

noBooleanSortComparator (JavaScript)

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

Disallow sort comparators that return a boolean.

Array#sort() and Array#toSorted() accept a comparator: a function that receives two items and returns a number. A negative number puts the first item before the second, a positive number puts it after, and zero keeps their current order.

When a comparator returns true or false, the sort method treats true as 1 and false as 0. The comparator can then never say that the first item comes first, so the items aren’t sorted correctly, and the result can differ between browsers and JavaScript runtimes.

Return a number instead: subtract numbers from each other, or use String#localeCompare() for strings.

This rule checks comparators whose body is a single expression or a single return statement, comparators with a boolean return type, and references to functions that return a boolean, such as Boolean or Number.isNaN. It doesn’t check comparators that contain several return statements.

array.sort((a, b) => a > b);
code-block.js:1:12 lint/nursery/noBooleanSortComparator  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This sort comparator returns a boolean instead of a number.

> 1 │ array.sort((a, b) => a > b);
│ ^^^^^^^^^^^^^^^
2 │

ℹ The sort method treats true as 1 and false as 0, so the comparator can never put the first item before the second. The items aren't sorted correctly, and the result can differ between JavaScript engines.

ℹ Return a negative number, zero, or a positive number instead, such as a - b for numbers or a.localeCompare(b) for strings.

ℹ 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: Subtract the compared values instead.

1 │ - array.sort((a,·b)·=>·a·>·b);
1 │ + array.sort((a,·b)·=>·a·-·b);
2 2 │

array.toSorted((a, b) => a.score >= b.score);
code-block.js:1:16 lint/nursery/noBooleanSortComparator  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This sort comparator returns a boolean instead of a number.

> 1 │ array.toSorted((a, b) => a.score >= b.score);
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The sort method treats true as 1 and false as 0, so the comparator can never put the first item before the second. The items aren't sorted correctly, and the result can differ between JavaScript engines.

ℹ Return a negative number, zero, or a positive number instead, such as a - b for numbers or a.localeCompare(b) for strings.

ℹ 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: Subtract the compared values instead.

1 │ - array.toSorted((a,·b)·=>·a.score·>=·b.score);
1 │ + array.toSorted((a,·b)·=>·a.score·-·b.score);
2 2 │

strings.sort((a, b) => a === b);
code-block.js:1:14 lint/nursery/noBooleanSortComparator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This sort comparator returns a boolean instead of a number.

> 1 │ strings.sort((a, b) => a === b);
│ ^^^^^^^^^^^^^^^^^
2 │

ℹ The sort method treats true as 1 and false as 0, so the comparator can never put the first item before the second. The items aren't sorted correctly, and the result can differ between JavaScript engines.

ℹ Return a negative number, zero, or a positive number instead, such as a - b for numbers or a.localeCompare(b) for strings.

ℹ 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.

array.sort((a: number, b: number): boolean => compare(a, b));
code-block.ts:1:12 lint/nursery/noBooleanSortComparator ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This sort comparator returns a boolean instead of a number.

> 1 │ array.sort((a: number, b: number): boolean => compare(a, b));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ The sort method treats true as 1 and false as 0, so the comparator can never put the first item before the second. The items aren't sorted correctly, and the result can differ between JavaScript engines.

ℹ Return a negative number, zero, or a positive number instead, such as a - b for numbers or a.localeCompare(b) for strings.

ℹ 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.

array.sort((a, b) => a - b);
array.toSorted((a, b) => a.score - b.score);
strings.sort((a, b) => a.localeCompare(b));
array.sort((a, b) => (a > b ? 1 : a < b ? -1 : 0));