DocsGuides

Guides

Sorting Guide

How to use, customize, or disable sorting features in Shadcn Table

Shadcn Table supports almost any sorting scenario you may have. Client-side sorting is enabled by default, but you can opt to implement your own server-side sorting logic or even replace the default client-side sorting with your own implementation.

Example

Relevant Table Options

Prop NameTypeDescription
enableGlobalFilterRankedResultsboolean
enableMultiSortboolean
enableSortPopoverbooleanAdds the sort button to the toolbar. It opens the sort editor, where the user adds, reorders and removes sort rules.
enableSortingboolean
enableSortingRemovalboolean
isMultiSortEvent(e: unknown) => booleanPass a custom function that will be used to determine if a multi-sort event should be triggered. It is passed the event from the sort toggle handler and should return true if the event should trigger a multi-sort.
manualSortingbooleanEnables manual sorting for the table. If this is true, you will be expected to sort your data before it is passed to the table. This is useful if you are doing server-side sorting.
maxMultiSortColCountnumberSet a maximum number of columns that can be multi-sorted.
onSortingChangeOnChangeFn<SortingState>If provided, this function will be called with an updaterFn when state.sorting changes. This overrides the default internal state management, so you will need to persist the state change either fully or partially outside of the table.
showSortIndexbooleanShows each sorted column's 1-based position next to its sort arrow while more than one sort is active.
sortDescFirstbooleanSet to true for sorting toggles on this column to start in the descending direction.
sortFnsRecord<string, SortFn>This option allows you to define custom sorting functions that can be referenced in a column's sortFn option by their key. TanStack Table core resolves the names: a sortFn string must be a built-in name or a key registered here, and an unknown name sorts nothing.

Relevant Column Options

Column OptionTypeDescription
enableMultiSortboolean
enableSortingboolean
invertSortingboolean
sortDescFirstboolean
sortFnSortFnOptionThe sorting function to use for this column: the name of a built-in or custom (sortFns table option) sorting function, or a sorting function itself. TanStack Table core resolves the name, and an unknown name sorts nothing.
sortUndefined'first' | 'last' | false | 1 | -1

Relevant State Options

State OptionTypeDescription
sortingArray<{ id: string, desc: boolean }>

Disable Sorting

Sorting can be disabled globally by setting the enableSorting table option to false. This will disable sorting for all columns. You can also disable sorting for individual columns by setting the enableSorting column option to false.

const columns = [
  {
    accessorKey: 'name',
    header: 'Name',
    enableSorting: false, // disable sorting for this column
  },
];

const table = useShadcnTable({
  columns,
  data,
  enableSorting: false, //disable sorting for all columns
});

Initial/Default Sorting

You can sort by a column or multiple columns by default by setting the sorting state option in either the initialState or state props.

const table = useShadcnTable({
  columns,
  data,
  initialState: {
    sorting: [
      {
        id: 'age', //sort by age by default on page load
        desc: true,
      },
      {
        id: 'lastName', //then sort by lastName if age is the same
        desc: true,
      },
    ],
  },
});

Default Sorting Features

Client-side sorting is enabled by default. When sorting is turned on for a column, the table will be sorted by the basic sorting algorithm by default.

Users sort from two places: the column menu (Sort ascending, Sort descending, Clear sort) and the sort editor behind the sort button in the toolbar. Clicking a header label opens the column menu and does not sort. A sorted column shows a small arrow after its label; an unsorted column shows nothing.

Multi-Sorting

Multi-sorting is also enabled by default, which means you can sort by multiple columns at once. No modifier key is needed: in the column menu, choosing a direction on a column that is already sorted replaces its direction, and choosing a direction on another column adds it after the current sorts. In the sort editor, "Add sort" adds a rule and the rules can be dragged into a new order. Set showSortIndex: true to show each sorted column's position next to its arrow while more than one sort is active.

const table = useShadcnTable({
  columns,
  data,
  showSortIndex: true, //show 1, 2, 3 next to the sort arrows of a multi-column sort
});

isMultiSortEvent still decides whether a call to a column's getToggleSortingHandler() adds to the current sorts, which matters if you write a custom Header that sorts on click.

You can limit the number of columns that can be sorted at once by setting the maxMultiSortColCount prop, or you can disable multi-sorting entirely by setting the enableMultiSort table option to false.

Sorting Removal

By default, users can remove a sort on a column with "Clear sort" in the column menu, or with the remove button of its rule in the sort editor. You can disable removing a sort from the column menu by setting the enableSortingRemoval table option to false.

const table = useShadcnTable({
  columns,
  data,
  enableSortingRemoval: false, //the column menu shows no Clear sort item
});

Sort Direction

By default, columns with string datatypes will sort alphabetically in ascending order, but columns with number datatypes will sort numerically in descending order. You can change the default sort direction per column by specifying the sortDescFirst column option to either true or false. You can also change the default sort direction globally by setting the sortDescFirst table option to either true or false.

Sorting Functions

By default, Shadcn Table will use the basic sorting function for all columns.

There are six built-in sorting functions you can choose from: alphanumeric, alphanumericCaseSensitive, text, textCaseSensitive, datetime, and basic. You can learn more about these built-in sorting functions in the TanStack Table Sorting docs.

Add Custom Sorting Functions

If none of these sorting functions meet your needs, you can add your own custom sorting functions by specifying more sorting functions in the sortFns table option.

const table = useShadcnTable({
  columns,
  data,
  sortFns: {
    //will add a new sorting function to the list of other sorting functions already available
    myCustomSortingFn: (rowA, rowB, columnId) => // your custom sorting logic
  },
})

Change Sorting Function Per Column

You can now choose a sorting function for each column by either passing a string value of the built-in sorting function names to the sortFn column option or by passing a custom sorting function to the sortFn column option.

const columns = [
  {
    accessorKey: 'name',
    header: 'Name',
    sortFn: 'textCaseSensitive', //use the built-in textCaseSensitive sorting function instead of the default basic sorting function
  },
  {
    accessorKey: 'age',
    header: 'Age',
    //use your own custom sorting function instead of any of the built-in sorting functions
    sortFn: (rowA, rowB, columnId) => // your custom sorting logic
  },
];

Manual Server-Side Sorting

If you are working with large data sets, you may want to let your back-end APIs handle all of the sorting and pagination processing instead of doing it client-side. You can do this by setting the manualSorting table option to true. This will disable the default client-side sorting and pagination features and will let you implement your own sorting and pagination logic.

When manualSorting is set to true, Shadcn Table assumes that your data is already sorted by the time you are passing it to the table.

If you need to sort your data in a back-end API, then you will also probably need access to the internal sorting state from the table. You can do this by managing the sorting state yourself and then passing it to the table via the state table option. You can also pass a callback function to the onSortingChange prop, which will be called whenever the sorting state changes internally in the table

const [sorting, setSorting] = useState([]);

const table = useShadcnTable({
  columns,
  data,
  manualSorting: true,
  state: { sorting },
  onSortingChange: setSorting,
});

useEffect(() => {
  //do something with the sorting state when it changes
  //or use table.state.sorting
}, [sorting]);

return <ShadcnTable table={table} />;

Remote Sorting Example

Here is the full Remote Data example showing how to implement server-side sorting, filtering, and pagination with Shadcn Table.

Storybook

View Extra Storybook Examples

On this page