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();