Basic sorting
Set enableSorting: true on columns that should be sortable. Click a sortable column header
to cycle through ascending, descending and unsorted states:
Sort indicators
A sorted column displays an arrow. Other sortable columns display an indicator only while
the header is hovered or focused, set sortIndicator="always" to keep it visible.
Set sortIndicatorPosition="inline" to place the indicator next to the label instead of the
far edge of the header cell:
Multi-column sort
Hold Shift and click column headers to sort by multiple columns. Pass enableMultiSort: false
to useDataGrid to disable it, or maxMultiSortColCount to limit the number of sorted columns:
Controlled sorting
Pass sorting and onSortingChange to useDataGrid to control sorting state.
The state is an array of { id: string, desc: boolean } objects:
Current sorting: []
Custom sort icons
Use sortedIcon and unsortedIcon props to replace the default indicators. sortedIcon must
point down: the same icon is rotated for ascending order:
Column menu
With withColumnMenu set on DataGrid, every sortable column gets Sort ascending,
Sort descending and Clear sort items in its column menu, see
Customization.
Custom sort functions
Set sortFn on a column definition to change how the column is sorted. The value is the name
of a built-in sort function or a comparator function:
Built-in sort functions
Built-in sort functions of TanStack Table that can be used as sortFn:
Sort removal behavior
Pass enableSortingRemoval: false to useDataGrid to remove the unsorted state from the cycle:
Sort direction
By default, the first click sorts text columns ascending and numeric columns descending.
Pass sortDescFirst: true to useDataGrid to sort every column descending on the first click,
or set sortDescFirst on a column definition to override a single column:
Server-side sorting
Set manualSorting: true and sort the data in your data fetching logic.
See the Server-side page for a complete example.
Accessibility
Sortable column headers are buttons that can be activated with Enter or Space. The header
cell has the aria-sort attribute. Headers of columns that are not sortable cannot be focused.