# prefer-simple-sort-comparator

Rule `unicorn/prefer-simple-sort-comparator`. Prefer a simple comparison function for `Array#sort()`.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #992 by reach, #1247 by growth

## Linters

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

## Code examples

### Incorrect: the rule reports this

```js
array.sort((a, b) => {
  if (a > b) {
    return 1;
  }

  if (a < b) {
    return -1;
  }

  return 0;
});
```

```js
array.sort((a, b) => a > b ? 1 : -1);
```

```js
array.sort((a, b) => a.foo > b.foo ? -1 : 1);
```

### Correct: the rule accepts this

```js
array.sort((a, b) => a - b);
```

```js
array.sort((a, b) => b.foo - a.foo);
```

```js
array.sort((a, b) => a.localeCompare(b));
```

Examples from the ESLint documentation.

## Enable it

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

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

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-simple-sort-comparator": "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% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [unicorn/prefer-short-escape-sequences](https://lintradar.dev/rules/unicorn/prefer-short-escape-sequences.md): Prefer shorter alternatives to Unicode escape sequences.
- [unicorn/prefer-simplified-conditions](https://lintradar.dev/rules/unicorn/prefer-simplified-conditions.md): Prefer simplified conditions.
- [unicorn/prefer-queue-microtask](https://lintradar.dev/rules/unicorn/prefer-queue-microtask.md): Prefer `queueMicrotask()` over `process.nextTick()`, `setImmediate()`, and `setTimeout(…, 0)`.
- [unicorn/prefer-single-array-predicate](https://lintradar.dev/rules/unicorn/prefer-single-array-predicate.md): Prefer a single `Array#some()` or `Array#every()` with a combined predicate.
- [unicorn/prefer-promise-with-resolvers](https://lintradar.dev/rules/unicorn/prefer-promise-with-resolvers.md): Prefer `Promise.withResolvers()` when extracting resolver functions from `new Promise()`.
- [unicorn/prefer-single-replace](https://lintradar.dev/rules/unicorn/prefer-single-replace.md): Enforce combining multiple single-character replacements into a single `String#replaceAll()` with a regular expression.

---

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