0Pricing
React Academy · Lesson

Sorting, Filtering, and Pagination

Enable column sorting, global and column filtering, and client-side pagination with TanStack Table features.

Sorting, Filtering, and Pagination is a free React Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the React Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Enabling Sorting

To add sorting, import getSortedRowModel and add it to your useReactTable config. You also need a sorting state (an array of {id, desc} objects) and an onSortingChange handler wired to React state.

The sorted row model wraps the core row model, reordering rows based on the current sorting state.

Toggling Sort on a Column Header

Each column header exposes column.getToggleSortingHandler(), which you attach to the onClick of your th element. Clicking cycles through ascending, descending, and unsorted states.

column.getIsSorted() returns 'asc', 'desc', or false, letting you render the appropriate sort indicator arrow.

Multi-Column Sorting

TanStack Table supports sorting by multiple columns simultaneously. Users hold the Shift key while clicking a second column header to add it to the sort array without removing the first.

The sortingState array contains all active sort columns in priority order, and the sorted row model applies them sequentially.

Global Filtering

Enable global filtering by importing getFilteredRowModel and adding globalFilter state plus an onGlobalFilterChange handler. Connect an input element to the state to filter across all columns at once.

The global filter uses a fuzzy-match or includes strategy by default, configurable via filterFns.

Column-Level Filtering

Individual columns can expose their own filters via column.getFilterValue() and column.setFilterValue(). Add a filter input below each column header and wire it to these methods.

Column filters and global filters stack: rows must pass all active filters to appear in the filtered row model.

Enabling Pagination

Import getPaginationRowModel and add it to the useReactTable config along with a pagination state object (pageIndex and pageSize) and an onPaginationChange handler.

The pagination row model slices the filtered (and sorted) rows to only the current page's slice.

Navigating Pages

The table instance exposes table.nextPage(), table.previousPage(), table.firstPage(), and table.lastPage() for navigation. Use table.getCanNextPage() and table.getCanPreviousPage() to disable buttons at the boundaries.

table.setPageIndex(n) jumps directly to a specific page; table.setPageSize(n) changes how many rows appear per page.

Pagination Info

table.getPageCount() returns the total number of pages based on the filtered row count divided by pageSize. table.getState().pagination.pageIndex gives the current zero-based page index.

Use these values to build a page counter like "Page 3 of 12" in your pagination UI.

Combining Row Models

Row models compose: getCoreRowModel provides the base, getFilteredRowModel wraps it applying filters, getSortedRowModel wraps filtered data applying sort, and getPaginationRowModel slices the result for the current page.

The order matters: sorting happens after filtering, pagination happens last.

Performance Considerations

TanStack Table memoizes row model computations internally. Sorting and filtering large datasets is efficient because only changed row models are recomputed.

For extremely large datasets (100,000+ rows), combine pagination with virtualization (react-virtual) to avoid rendering all page rows at once.

Sort, Filter, and Paginate Summary

Wire getSortedRowModel with sorting state, getFilteredRowModel with globalFilter and column filter states, and getPaginationRowModel with pagination state. Each feature is opt-in and composes cleanly with the others.

Column headers drive sort via getToggleSortingHandler; inputs drive filter via setFilterValue and onGlobalFilterChange; buttons drive pagination via nextPage, previousPage, setPageIndex.

TanStack Table Pagination State

Which object holds the current page index and page size in TanStack Table's pagination state?

Lesson Recap

Sorting requires getSortedRowModel, sorting state, and column header click handlers. Filtering uses getFilteredRowModel with global and per-column filter state. Pagination uses getPaginationRowModel with pageIndex and pageSize state.

All three row models compose automatically: filter first, sort second, paginate last.

Frequently asked questions

Is the “Sorting, Filtering, and Pagination” lesson free?

Yes — the full text of “Sorting, Filtering, and Pagination” is free to read here on the web, and the React Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the React Academy course, upgrade to CoddyKit PRO.

What will I learn in “Sorting, Filtering, and Pagination”?

Enable column sorting, global and column filtering, and client-side pagination with TanStack Table features. You practise React Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start React Academy?

No prior experience is required. React Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Sorting, Filtering, and Pagination” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this React Academy lesson?

Yes. Every React Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. TanStack Table Philosophy and Headless Design
  2. Defining Columns and Rendering Rows
  3. Sorting, Filtering, and Pagination
  4. Row Selection and Virtualized Tables
← Back to React Academy