# @typescript-eslint/require-array-sort-compare

> Require a compare function in calls to `sort` and `toSorted` on arrays

Without a compare function, `sort()` and `toSorted()` turn every element into a string and compare the strings. `[1, 2, 10].sort()` gives `[1, 10, 2]`.

Arrays of strings are allowed by default, because sorting them as strings is usually what you want.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/require-array-sort-compare" = "error" # or "warn"
```

Every option, set to its default:

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/require-array-sort-compare" = ["error", {
  ignoreStringArrays = true,
}]
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
const prices = [5, 100, 20];

prices.sort(); // [100, 20, 5]
const sorted = prices.toSorted();
```

Not reported:

{/* good */}
```ts
const prices = [5, 100, 20];
prices.sort((a, b) => a - b); // [5, 20, 100]

const names = ["Zoe", "Adam"];
names.sort();
```

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `ignoreStringArrays` | `true` | Allow `sort()` with no argument on an array where every element is a string. |

{/* bad {"options": [{"ignoreStringArrays": false}]} */}
```ts
const names = ["Zoe", "Adam"];
names.sort();
```
