Skip to content

arrayMutableSorts

Reports .sort() calls on arrays that mutate the original array.

✅ This rule is included in the ts stylistic preset.

The .sort() method mutates the original array in place. Using .toSorted() instead returns a new sorted array without modifying the original, making code more predictable and avoiding unintended side effects.

const values = [3, 1, 2];
const sorted = values.sort();
const items = [3, 1, 2];
const sorted = items.sort((a, b) => a - b);
const data = [3, 1, 2];
const result = [...data].sort();

This rule is not configurable.

If you intentionally want to mutate the original array in place, or if you’re working in an environment that doesn’t support .toSorted() (ES2023+), you may want to disable this rule. Note that .sort() is still allowed when used as a standalone expression statement since the mutation is likely intentional in that case.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.