Sorting

DataGrid sorting features

Basic sorting

Set enableSorting: true on columns that should be sortable. Click a sortable column header to cycle through ascending, descending and unsorted states:

Start Date
AliceJohnsonEngineering950002021-03-15
BobSmithMarketing720002020-07-01
CarolWilliamsSales680002022-01-10
DavidBrownHR650002019-11-20
EvaDavisFinance880002021-06-05
FrankMillerEngineering1020002018-09-12
GraceWilsonMarketing760002022-04-18
HenryTaylorSales710002020-02-28
IreneAndersonEngineering970002021-08-30
JamesThomasFinance910002019-05-14
KarenJacksonHR630002023-01-09
LeoWhiteEngineering1100002017-12-01
MiaHarrisSales740002022-07-22
NathanMartinMarketing690002021-10-03
OliviaGarciaFinance850002020-04-11
PatrickMartinezEngineering990002019-08-25
QuinnRobinsonHR670002022-11-14
RachelClarkSales730002021-02-07
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableSorting: true },
      { accessorKey: 'lastName', header: 'Last Name', enableSorting: true },
      { accessorKey: 'department', header: 'Department', enableSorting: true },
      { accessorKey: 'salary', header: 'Salary', enableSorting: true },
      { accessorKey: 'startDate', header: 'Start Date' },
    ],
  });

  return <DataGrid store={store} />;
}

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:

FrankMilleractive102000
AliceJohnsonactive95000
EvaDavisactive88000
BobSmithactive72000
CarolWilliamsactive68000
DavidBrowninactive65000
Sort indicator
Sort indicator position
import { useState } from 'react';
import { SortingState } from '@tanstack/react-table';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';
function Demo() {
  const [sorting, setSorting] = useState<SortingState>([{ id: 'salary', desc: true }]);
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableSorting: true },
      { accessorKey: 'lastName', header: 'Last Name', enableSorting: true },
      { accessorKey: 'status', header: 'Status', enableSorting: true, meta: { align: 'center' } },
      { accessorKey: 'salary', header: 'Salary', enableSorting: true, meta: { align: 'end' } },
    ],
    sorting,
    onSortingChange: setSorting,
  });

  return (
    <DataGrid
      store={store}
    />
  );
}

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:

AliceJohnsonEngineering95000active
BobSmithMarketing72000active
CarolWilliamsSales68000active
DavidBrownHR65000inactive
EvaDavisFinance88000active
FrankMillerEngineering102000active
GraceWilsonMarketing76000active
HenryTaylorSales71000inactive
IreneAndersonEngineering97000active
JamesThomasFinance91000active
KarenJacksonHR63000active
LeoWhiteEngineering110000active
MiaHarrisSales74000active
NathanMartinMarketing69000inactive
OliviaGarciaFinance85000active
PatrickMartinezEngineering99000active
QuinnRobinsonHR67000active
RachelClarkSales73000active
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableSorting: true },
      { accessorKey: 'lastName', header: 'Last Name', enableSorting: true },
      { accessorKey: 'department', header: 'Department', enableSorting: true },
      { accessorKey: 'salary', header: 'Salary', enableSorting: true },
      { accessorKey: 'status', header: 'Status', enableSorting: true },
    ],
    tableOptions: { enableMultiSort: true },
  });

  return <DataGrid store={store} />;
}

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: []

Status
AliceJohnsonEngineering95000active
BobSmithMarketing72000active
CarolWilliamsSales68000active
DavidBrownHR65000inactive
EvaDavisFinance88000active
FrankMillerEngineering102000active
GraceWilsonMarketing76000active
HenryTaylorSales71000inactive
IreneAndersonEngineering97000active
JamesThomasFinance91000active
KarenJacksonHR63000active
LeoWhiteEngineering110000active
MiaHarrisSales74000active
NathanMartinMarketing69000inactive
OliviaGarciaFinance85000active
PatrickMartinezEngineering99000active
QuinnRobinsonHR67000active
RachelClarkSales73000active
import { useState } from 'react';
import { SortingState } from '@tanstack/react-table';
import { Code, Text } from '@mantine/core';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const [sorting, setSorting] = useState<SortingState>([]);

  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableSorting: true },
      { accessorKey: 'lastName', header: 'Last Name', enableSorting: true },
      { accessorKey: 'department', header: 'Department', enableSorting: true },
      { accessorKey: 'salary', header: 'Salary', enableSorting: true },
      { accessorKey: 'status', header: 'Status' },
    ],
    sorting,
    onSortingChange: setSorting,
  });

  return (
    <>
      <Text mb="md">
        Current sorting: <Code>{JSON.stringify(sorting)}</Code>
      </Text>
      <DataGrid store={store} />
    </>
  );
}

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:

AliceJohnsonEngineering95000
BobSmithMarketing72000
CarolWilliamsSales68000
DavidBrownHR65000
EvaDavisFinance88000
FrankMillerEngineering102000
GraceWilsonMarketing76000
HenryTaylorSales71000
IreneAndersonEngineering97000
JamesThomasFinance91000
KarenJacksonHR63000
LeoWhiteEngineering110000
MiaHarrisSales74000
NathanMartinMarketing69000
OliviaGarciaFinance85000
PatrickMartinezEngineering99000
QuinnRobinsonHR67000
RachelClarkSales73000
import { ArrowDownIcon, ArrowsDownUpIcon } from '@phosphor-icons/react';
import { DataGrid, useDataGrid } from '@mantine/data-grid';
import { employees } from './data';

function Demo() {
  const store = useDataGrid({
    data: employees,
    columns: [
      { accessorKey: 'firstName', header: 'First Name', enableSorting: true },
      { accessorKey: 'lastName', header: 'Last Name', enableSorting: true },
      { accessorKey: 'department', header: 'Department', enableSorting: true },
      { accessorKey: 'salary', header: 'Salary', enableSorting: true, meta: { align: 'end' } },
    ],
  });

  return (
    <DataGrid
      store={store}
      sortIndicator="always"
      sortedIcon={<ArrowDownIcon size={14} />}
      unsortedIcon={<ArrowsDownUpIcon size={14} />}
    />
  );
}

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:

const columns = [
  {
    accessorKey: 'date',
    header: 'Date',
    enableSorting: true,
    sortFn: 'datetime',
  },
  {
    accessorKey: 'name',
    header: 'Name',
    enableSorting: true,
    sortFn: (rowA, rowB, columnId) => {
      return rowA.getValue(columnId).localeCompare(rowB.getValue(columnId));
    },
  },
];

Built-in sort functions

Built-in sort functions of TanStack Table that can be used as sortFn:

NameDescription
alphanumericCase-insensitive alphanumeric sort
alphanumericCaseSensitiveCase-sensitive alphanumeric sort
textCase-insensitive string sort
textCaseSensitiveCase-sensitive string sort
datetimeDate/time sort
basicBasic sort using JavaScript comparison

Sort removal behavior

Pass enableSortingRemoval: false to useDataGrid to remove the unsorted state from the cycle:

const store = useDataGrid({
  data,
  columns,
  enableSortingRemoval: false,
});

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:

const store = useDataGrid({
  data,
  columns,
  sortDescFirst: true,
});

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.