js-tosorted-immutable — Claude Code rule
The js-tosorted-immutable rule replaces mutating .sort() with .toSorted(), keeping React props and state arrays unchanged during sorting.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/js-tosorted-immutable.mdWhat js-tosorted-immutable does
The rule directs Claude Code to use .toSorted() when an array needs ordering. Unlike .sort(), it returns a new sorted array and leaves the original value intact. That fits React’s read-only treatment of props and state.
It also addresses array changes inside callbacks and effects, where mutation can produce stale behavior. Older browsers and runtimes below Node.js 20 need a fallback such as [...items].sort((a, b) => a.value - b.value). Projects running Node.js 20+ can use .toSorted() directly.
What it helps with
- replace in-place array sorting
- keep React props unchanged
- avoid stale arrays inside closures
- rewrite user-list sorting safely
Example prompts for the agent
- Review this React code and replace
.sort()with a non-mutating sort. - Rewrite the
userslist sorting without changing the props array. - Check whether this project needs a fallback for
.toSorted().
How to install js-tosorted-immutable
Manually: put the files in place
.claude/rules/js-tosorted-immutable.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/js-tosorted-immutable.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/js-tosorted-immutable.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/js-tosorted-immutable.mdManually: put the files in place
.clinerules/js-tosorted-immutable.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about js-tosorted-immutable
How do I install js-tosorted-immutable in Claude Code?
Place .claude/rules/js-tosorted-immutable.md in the project.
What does js-tosorted-immutable do?
It tells the coding agent to prefer .toSorted() over .sort(), so sorting returns a new array instead of changing the original. The rule targets mutation problems involving React props, state, callbacks, and effects.
What is the fallback for older runtimes?
Copy the array before calling .sort(), as in [...items].sort((a, b) => a.value - b.value). .toSorted() is available in Chrome 110+, Safari 16+, Firefox 115+, and Node.js 20+.
Source text
js-tosorted-immutable.md · MIT
---
title: Use toSorted() Instead of sort() for Immutability
impact: MEDIUM-HIGH
impactDescription: prevents mutation bugs in React state
tags: javascript, arrays, immutability, react, state, mutation
---
## Use toSorted() Instead of sort() for Immutability
`.sort()` mutates the array in place, which can cause bugs with React state and props. Use `.toSorted()` to create a new sorted array without mutation.
**Incorrect (mutates original array):**
```typescript
function UserList({ users }: { users: User[] }) {
// Mutates the users prop array!
const sorted = useMemo(
() => users.sort((a, b) => a.name.localeCompare(b.name)),
[users]
)
return <div>{sorted.map(renderUser)}</div>
}
```
**Correct (creates new array):**
```typescript
function UserList({ users }: { users: User[] }) {
// Creates new sorted array, original unchanged
const sorted = useMemo(
() => users.toSorted((a, b) => a.name.localeCompare(b.name)),
[users]
)
return <div>{sorted.map(renderUser)}</div>
}
```
**Why this matters in React:**
1. Props/state mutations break React's immutability model - React expects props and state to be treated as read-only
2. Causes stale closure bugs - Mutating arrays inside closures (callbacks, effects) can lead to unexpected behavior
**Browser support (fallback for older browsers):**
`.toSorted()` is available in all modern browsers (Chrome 110+, Safari 16+, Firefox 115+, Node.js 20+). For older environments, use spread operator:
```typescript
// Fallback for older browsers
const sorted = [...items].sort((a, b) => a.value - b.value)
```
**Other immutable array methods:**
- `.toSorted()` - immutable sort
- `.toReversed()` - immutable reverse
- `.toSpliced()` - immutable splice
- `.with()` - immutable element replacement
© tech-leads-club · MIT · tech-leads-club/agent-skills