# require-array-sort-compare

Rule `unicorn/require-array-sort-compare`. Require a compare function when calling `Array#sort()` or `Array#toSorted()`.

- Source: unicorn
- Technologies: JavaScript
- Kind: correctness
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.5M weekly downloads of presets that enable it
- Trend: +30.3% last quarter
- Rank: #1120 by reach, #1139 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/require-array-sort-compare` | no | suggestion | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/require-array-sort-compare.md) |

## Code examples

### Incorrect: the rule reports this

```js
const numbers = [3, 1, 10, 2, 20];
numbers.toSorted();
```

```js
[5, 10, 15, 2, 25].sort();
// → [10, 15, 2, 25, 5] (unexpected!)
```

```js
const names = ['Alice', 'bob', 'Charlie'];
names.sort();
// → ['Alice', 'Charlie', 'bob'] (case-sensitive)
```

### Correct: the rule accepts this

```js
const numbers = [3, 1, 10, 2, 20];
numbers.toSorted((a, b) => a - b);
```

```js
[5, 10, 15, 2, 25].sort((a, b) => a - b);
// → [2, 5, 10, 15, 25]
```

```js
const names = ['Alice', 'bob', 'Charlie'];
names.sort((a, b) => a.localeCompare(b, undefined, {sensitivity: 'base'}));
// → ['Alice', 'bob', 'Charlie']
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/require-array-sort-compare": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [Unicorn unopinionated](https://lintradar.dev/presets/unicorn-unopinionated.md) | ESLint | `eslint-plugin-unicorn` | 236 | 9.5M | +30.3% |

## Related rules

- [typescript/require-array-sort-compare](https://lintradar.dev/rules/typescript/require-array-sort-compare.md): Require `Array#sort` and `Array#toSorted` calls to always provide a `compareFunction`
- [unicorn/no-useless-template-literals](https://lintradar.dev/rules/unicorn/no-useless-template-literals.md): Disallow useless template literal expressions.
- [unicorn/no-this-outside-of-class](https://lintradar.dev/rules/unicorn/no-this-outside-of-class.md): Disallow `this` outside of classes.
- [unicorn/no-invalid-argument-count](https://lintradar.dev/rules/unicorn/no-invalid-argument-count.md): Disallow calling functions and constructors with an invalid number of arguments.
- [unicorn/no-unreadable-for-of-expression](https://lintradar.dev/rules/unicorn/no-unreadable-for-of-expression.md): Disallow unreadable iterable expressions in `for…of` and `for await…of` loop headers.
- [unicorn/consistent-compound-words](https://lintradar.dev/rules/unicorn/consistent-compound-words.md): Enforce consistent spelling of compound words in identifiers.

---

Web page: https://lintradar.dev/rules/unicorn/require-array-sort-compare. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
