Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Filter and Sort a List in React?

ReactJavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 29, 2026
6 min read

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.

Table of Contents

  • Why you derive the visible list instead of storing it
  • Filtering with filter()
  • Sorting safely with a copy
  • Putting it together in a component
  • Handling ties, empty results and large lists
  • Common mistakes to avoid
  • Frequently Asked Questions
    • Should I use useEffect to filter a list?
    • Is toSorted() safe to use in every browser?
    • How do I filter on several fields at once?
    • How do I make search wait until the user stops typing?
  • Summary
  • Related Articles

Why you derive the visible list instead of storing it

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.

Filtering with 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.

javascript
javascript
Loading syntax highlighting...
text

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.

Sorting safely with a copy

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

javascript
javascript
Loading syntax highlighting...

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 <.

Putting it together in a component

Here is a complete component with a search box, a sort selector and the derived list.

javascript
javascript
Loading syntax highlighting...

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.

Handling ties, empty results and large lists

A few details make the list feel finished.

  • Sorting by a value that can repeat (such as a category) should add a second rule, for example a.category.localeCompare(b.category) || a.name.localeCompare(b.name). The || falls through to the second rule only when the first returns 0.
  • Always show a message when the filtered list is empty, so the user does not think the page is broken.
  • For a descending order, swap the arguments (b.price - a.price) rather than reversing the array afterwards.
  • For a few thousand items, the calculation is fast enough to run on every render. If measurements show it is slow, wrap it in useMemo with [query, sortBy] as dependencies. Do not add useMemo before you have measured a problem.

Common mistakes to avoid

MistakeWhy it hurtsFix
Calling products.sort() on state or propsMutates data React treats as read-onlyUse toSorted() or [...products].sort()
Storing the filtered list in stateTwo sources of truth that drift apartCompute it during render
Using the array index as keyRows get mixed up after sorting or filteringUse a stable id
Comparing strings with a.name - b.nameReturns NaN, so the order is undefinedUse localeCompare()

Frequently Asked Questions

Should I use 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.

Is 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).

How do I filter on several fields at once?

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.

How do I make search wait until the user stops typing?

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.

Summary

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.

Related Articles

  • Why Do React Lists Need a Unique key Prop?
  • What Is State in React and Why Is It Needed?
  • Learn React 19: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How Do You Build a Counter App in React Step by Step?
  • How Do You Use the useState Hook in React?
  • What Is State in React and Why Is It Needed?
  • Why Do React Lists Need a Unique key Prop?
  • How Do You Render a List with map() in React?
  • How Do You Use Ternary and && Operators in JSX?
  • How Do You Conditionally Render Components in React?
  • How Do You Pass Arguments to Event Handlers in React?