πΌ 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.
// β
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);Type: object
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();