# no-alphabetical-sort

Rule `sonarjs/no-alphabetical-sort`. "Array.prototype.sort()" and "Array.prototype.toSorted()" should use a compare function

- Source: sonarjs
- Technologies: JavaScript
- Kind: correctness
- Needs type information: yes
- Autofix: yes, in at least one linter
- Reach: 6.2M weekly downloads of presets that enable it
- Trend: +62.1% last quarter
- Rank: #1239 by reach, #540 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/no-alphabetical-sort` | yes | suggestion | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-2871/) |

## Code examples

### Incorrect: the rule reports this

```js
var myarray = [80, 3, 9, 34, 23, 5, 1];

myarray.sort();
console.log(myarray); // outputs: [1, 23, 3, 34, 5, 80, 9]
```

### Correct: the rule accepts this

```js
var myarray = [80, 3, 9, 34, 23, 5, 1];

myarray.sort((a, b) => (a - b));
console.log(myarray); // outputs: [1, 3,  5, 9, 23, 34, 80]
```

Examples from the ESLint documentation.

## Enable it

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

```js
export default [
  {
    rules: {
      "sonarjs/no-alphabetical-sort": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [SonarJS recommended](https://lintradar.dev/presets/sonarjs-recommended.md) | ESLint | `eslint-plugin-sonarjs` | 230 | 6.2M | +62.1% |

## Related rules

- [sonarjs/no-all-duplicated-branches](https://lintradar.dev/rules/sonarjs/no-all-duplicated-branches.md): All branches in a conditional structure should not have exactly the same implementation
- [sonarjs/no-angular-bypass-sanitization](https://lintradar.dev/rules/sonarjs/no-angular-bypass-sanitization.md): Angular built-in sanitization should not be disabled
- [sonarjs/new-operator-misuse](https://lintradar.dev/rules/sonarjs/new-operator-misuse.md): "new" should only be used with functions and classes
- [sonarjs/no-array-delete](https://lintradar.dev/rules/sonarjs/no-array-delete.md): "delete" should not be used on arrays
- [sonarjs/misplaced-loop-counter](https://lintradar.dev/rules/sonarjs/misplaced-loop-counter.md): "for" loop increment clauses should modify the loops' counters
- [sonarjs/no-associative-arrays](https://lintradar.dev/rules/sonarjs/no-associative-arrays.md): Array indexes should be numeric

---

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