sort or toSorted is called without a compare function and orders elements as strings

tx0/ts:sort-without-comparatorstrictwarningsmell

What it finds

scores.sort(): without a compare function, sort and toSorted convert the elements to strings and order them by UTF-16 code units, so [10, 9, 1] becomes [1, 10, 9] and names sort upper case before lower case. A compare function, as (a, b) => a - b or a.localeCompare(b), says which order is meant.

Arrays of strings that are meant to be in code unit order are sorted this way on purpose, which is why the rule is strict.

From the rule's source, sort_without_comparator.rs.

Example from the rule's tests
function order(scores: number[], names: string[]) {  scores.sort();tx0 reports: sort() without a compare function orders the elements as strings  const sorted = names.toSorted();tx0 reports: toSorted() without a compare function  return [scores.sort((a, b) => a - b), sorted];}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/ts:sort-without-comparator" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/ts:sort-without-comparator] reason="..."

tx0 explain tx0/ts:sort-without-comparator shows the same in the terminal. Found a false positive? Report it.