no-array-sort
March 27, 2026 ยท View on GitHub
๐ Prefer Array#toSorted() over Array#sort().
๐ผ This rule is enabled in the following configs: โ
recommended, โ๏ธ unopinionated.
๐ก This rule is manually fixable by editor suggestions.
Prefer using Array#toSorted() over Array#sort().
Array#sort() modifies the original array, while Array#toSorted() returns a new sorted array.
Examples
// โ
const sorted = [...array].sort();
// โ
const sorted = array.toSorted();
// โ
const sorted = [...iterable].sort();
// โ
const sorted = [...iterable].toSorted();
// โ
const sorted = [...array].sort((a, b) => a - b);
// โ
const sorted = array.toSorted((a, b) => a - b);
Options
Type: object
allowExpressionStatement
Type: boolean
Default: true
This rule allows array.sort() to be used as an expression statement by default.
Pass allowExpressionStatement: false to forbid Array#sort() even if it's an expression statement.
/* eslint unicorn/no-array-sort: ["error", {"allowExpressionStatement": false}] */
// โ
array.sort();