Shadcn React Table

Search documentation

Search the docs

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.

BasicSorting + client pagination — the default feature set.
GitHub
ID
AvaThompsonOwnerEngineeringactive22$45,000Jan 2, 2023
0%
LiamNguyenAdminMarketinginactive25$47,137Jan 13, 2023
9%
NoahSilvaEditorDesignpending28$49,274Jan 24, 2023
18%
EmmaCarterViewerSalesactive31$51,411Feb 4, 2023
27%
OliviaRossiOwnerSupportinactive34$53,548Feb 15, 2023
36%
WilliamWalkerAdminEngineeringpending37$55,685Feb 26, 2023
45%
SophiaPatelEditorMarketingactive40$57,822Mar 9, 2023
54%
JamesMullerViewerDesigninactive43$59,959Mar 20, 2023
63%
IsabellaParkOwnerSalespending46$62,096Mar 31, 2023
72%
LucasReyesAdminSupportactive49$64,233Apr 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.

tsx
const table = useDataTable({
  data,
  columns,
  enableMultiSort: true,
  initialState: {
    sorting: [
      { id: "department", desc: false },
      { id: "salary", desc: true },
    ],
  },
})
SortingMulti-column sorting (shift-click) with an order badge.
GitHub
ID
NoahSilvaEditorDesignactive28$134,754Apr 8, 2024
75%
LeoHaddadAdminDesigninactive53$124,069Feb 13, 2024
30%
CharlotteChenOwnerDesignpending38$113,384Dec 20, 2023
86%
JamesMullerViewerDesignactive23$102,699Oct 26, 2023
41%
NoahSilvaEditorDesigninactive48$92,014Sep 1, 2023
97%
LeoHaddadAdminDesignpending33$81,329Jul 8, 2023
52%
CharlotteChenOwnerDesignactive58$70,644May 14, 2023
7%
JamesMullerViewerDesigninactive43$59,959Mar 20, 2023
63%
JamesMullerViewerDesignpending43$55,439Jun 2, 2024
19%
NoahSilvaEditorDesignpending28$49,274Jan 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).
  • sortFn — a built-in sort function's name, a comparator, or the name of one of your own from the sortFns option.
tsx
const columns: DataTableColumnDef<Person>[] = [
  { accessorKey: "id", header: "ID", enableSorting: false },
  { accessorKey: "salary", header: "Salary", sortDescFirst: true },
]

To reuse a comparator across columns, register it by name with sortFns:

tsx
const PRIORITY = ["low", "medium", "high"]

const table = useDataTable({
  data,
  columns: [{ accessorKey: "priority", header: "Priority", sortFn: "byPriority" }],
  sortFns: {
    byPriority: (rowA, rowB, columnId) =>
      PRIORITY.indexOf(rowA.getValue(columnId)) -
      PRIORITY.indexOf(rowB.getValue(columnId)),
  },
})
Custom sort functionSort roles by rank with a named sort function.
GitHub
ID
AvaThompsonOwnerEngineeringactive22$45,000Jan 2, 2023
0%
OliviaRossiOwnerSupportinactive34$53,548Feb 15, 2023
36%
IsabellaParkOwnerSalespending46$62,096Mar 31, 2023
72%
CharlotteChenOwnerDesignactive58$70,644May 14, 2023
7%
HarperDuboisOwnerMarketinginactive30$79,192Jun 27, 2023
43%
AvaThompsonOwnerEngineeringpending42$87,740Aug 10, 2023
79%
OliviaRossiOwnerSupportactive54$96,288Sep 23, 2023
14%
IsabellaParkOwnerSalesinactive26$104,836Nov 6, 2023
50%
CharlotteChenOwnerDesignpending38$113,384Dec 20, 2023
86%
HarperDuboisOwnerMarketingactive50$121,932Feb 2, 2024
21%
$4,120,536
Rows per page
1–10 of 48

Controlled sorting

Pass state.sorting and onSortingChange to control it yourself — for example to sort on the server (manualSorting: true).

tsx
const [sorting, setSorting] = React.useState([])
const table = useDataTable({
  data,
  columns,
  state: { sorting },
  onSortingChange: setSorting,
  manualSorting: true,
})