noBooleanSortComparator (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.3 - Diagnostic Category:
lint/nursery/noBooleanSortComparator - This rule has an unsafe fix.
- The default severity of this rule is error.
- Sources:
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noBooleanSortComparator": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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));Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.