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.

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

const table = useDataTable({
  data,
  columns,
  enableMultiSort: true,
  initialState: {
    sorting: [
      { id: "department", desc: false },
      { id: "salary", desc: true },
    ],
  },
})
Sorting
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).
  • 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,
})