Sorting
Sorting is on by default. Click a column header to cycle ascending → descending → unsorted; the header shows the current direction and the column-actions menu offers explicit sort/clear items.
Basic
ID | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Ava | Thompson | Owner | Engineering | active | 22 | $45,000 | Jan 2, 2023 | 0% | ||
| Liam | Nguyen | Admin | Marketing | inactive | 25 | $47,137 | Jan 13, 2023 | 9% | ||
| Noah | Silva | Editor | Design | pending | 28 | $49,274 | Jan 24, 2023 | 18% | ||
| Emma | Carter | Viewer | Sales | active | 31 | $51,411 | Feb 4, 2023 | 27% | ||
| Olivia | Rossi | Owner | Support | inactive | 34 | $53,548 | Feb 15, 2023 | 36% | ||
| William | Walker | Admin | Engineering | pending | 37 | $55,685 | Feb 26, 2023 | 45% | ||
| Sophia | Patel | Editor | Marketing | active | 40 | $57,822 | Mar 9, 2023 | 54% | ||
| James | Muller | Viewer | Design | inactive | 43 | $59,959 | Mar 20, 2023 | 63% | ||
| Isabella | Park | Owner | Sales | pending | 46 | $62,096 | Mar 31, 2023 | 72% | ||
| Lucas | Reyes | Admin | Support | active | 49 | $64,233 | Apr 11, 2023 | 81% | ||
| $4,120,536 |
Rows per page
1–10 of 48
Multi-column sorting
Enable enableMultiSort to sort by several columns at once (shift-click headers). An order badge indicates each column's position in the sort.
const table = useDataTable({
data,
columns,
enableMultiSort: true,
initialState: {
sorting: [
{ id: "department", desc: false },
{ id: "salary", desc: true },
],
},
})
Sorting
ID | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Noah | Silva | Editor | Design | active | 28 | $134,754 | Apr 8, 2024 | 75% | ||
| Leo | Haddad | Admin | Design | inactive | 53 | $124,069 | Feb 13, 2024 | 30% | ||
| Charlotte | Chen | Owner | Design | pending | 38 | $113,384 | Dec 20, 2023 | 86% | ||
| James | Muller | Viewer | Design | active | 23 | $102,699 | Oct 26, 2023 | 41% | ||
| Noah | Silva | Editor | Design | inactive | 48 | $92,014 | Sep 1, 2023 | 97% | ||
| Leo | Haddad | Admin | Design | pending | 33 | $81,329 | Jul 8, 2023 | 52% | ||
| Charlotte | Chen | Owner | Design | active | 58 | $70,644 | May 14, 2023 | 7% | ||
| James | Muller | Viewer | Design | inactive | 43 | $59,959 | Mar 20, 2023 | 63% | ||
| James | Muller | Viewer | Design | pending | 43 | $55,439 | Jun 2, 2024 | 19% | ||
| Noah | Silva | Editor | Design | pending | 28 | $49,274 | Jan 24, 2023 | 18% | ||
| $4,120,536 |
Rows per page
1–10 of 48
Per-column control
Sorting is standard TanStack, so the usual column knobs apply:
enableSorting: false— disable sorting for a column.sortDescFirst: true— start from descending (handy for numeric columns).sortingFn— a custom comparator.
const columns: ColumnDef<Person>[] = [
{ accessorKey: "id", header: "ID", enableSorting: false },
{ accessorKey: "salary", header: "Salary", sortDescFirst: true },
]
Controlled sorting
Pass state.sorting and onSortingChange to control it yourself — for example to sort on the server (manualSorting: true).
const [sorting, setSorting] = React.useState([])
const table = useDataTable({
data,
columns,
state: { sorting },
onSortingChange: setSorting,
manualSorting: true,
})
Related
- useDataTable options —
enableMultiSortand other sort flags pass through to TanStack. - TanStack Table sorting guide.