To filter and sort a list in React, keep the original array untouched, derive a new array from it during rendering with filter() and a sorted copy, and then render that derived array with map(). The search text and the sort choice live in state, and the visible list is calculated from them on every render.
This approach is the usual way to filter and sort a list in React because it avoids a second copy of the data in state that could drift out of sync. This guide uses React 19 and function components.
filter()A list on screen usually depends on three things: the full data, a search term and a sort order. The full data is the source of truth. The search term and the sort order are small pieces of state. The visible list is not new information, so it should not be state. It is a value you compute while rendering.
If you stored the filtered list in state, you would have to update it every time the data, the search term or the sort order changed. Forgetting one update gives you a screen that disagrees with your data. Computing it during render removes that whole class of bug.
filter()Array.prototype.filter() returns a new array that contains only the items for which your callback returns true. It does not change the original array.
Lowercasing both sides makes the match case-insensitive. An empty query makes includes("") return true for every item, so the full list shows when the search box is empty, with no special case needed.
Array.prototype.sort() sorts the array in place and returns the same array. If you call it on an array that came from props or state, you mutate data React does not expect you to change. That can cause stale screens and confusing bugs.
Make a copy first. Modern JavaScript (ES2023) has toSorted(), which returns a new sorted array and leaves the original alone. In older environments, use [...items].sort(...).
The comparator receives two items and returns a negative number, zero or a positive number. For numbers, subtract one from the other. For text, use localeCompare(), which handles accents and language rules better than comparing with <.
Here is a complete component with a search box, a sort selector and the derived list.
The two inputs are controlled: their value comes from state and every change calls a setter. Each change re-renders the component, which recalculates visible. The key on each <li> is the stable id, not the array index, so React can track items correctly when the order changes.
A few details make the list feel finished.
a.category.localeCompare(b.category) || a.name.localeCompare(b.name). The || falls through to the second rule only when the first returns 0.b.price - a.price) rather than reversing the array afterwards.useMemo with [query, sortBy] as dependencies. Do not add useMemo before you have measured a problem.| Mistake | Why it hurts | Fix |
|---|---|---|
Calling products.sort() on state or props | Mutates data React treats as read-only | Use toSorted() or [...products].sort() |
| Storing the filtered list in state | Two sources of truth that drift apart | Compute it during render |
Using the array index as key | Rows get mixed up after sorting or filtering | Use a stable id |
Comparing strings with a.name - b.name | Returns NaN, so the order is undefined | Use localeCompare() |
useEffect to filter a list?No. An effect that copies a filtered list into state adds an extra render and an extra source of truth. Calculate the list directly in the component body instead.
toSorted() safe to use in every browser?It is part of ES2023 and works in current evergreen browsers and current Node.js versions. If you must support older runtimes, use [...items].sort(compare).
Combine conditions inside the callback, for example p.name.toLowerCase().includes(q) || p.category.toLowerCase().includes(q). Keep one search string in state and test it against each field.
Keep the input value in state for responsiveness, and keep a second, delayed value for filtering, using a debounce or React's useDeferredValue. For small lists this is not needed.
Store only the search text and the sort choice in state. Derive the visible list with filter() and a sorted copy (toSorted() or a spread plus sort()), then render it with map() and stable keys. Never sort state in place, and show a friendly message when nothing matches.