# Sorting
Package: @mantine/data-grid
Import: import { Sorting } from '@mantine/data-grid';
Description: 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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
// Demo.tsx
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}
       sortIndicator="hover" sortIndicatorPosition="trailing"
    />
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
// Demo.tsx
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} />;
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## Controlled sorting

Pass `sorting` and `onSortingChange` to `useDataGrid` to control sorting state.
The state is an array of `{ id: string, desc: boolean }` objects:

```tsx
// Demo.tsx
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} />
    </>
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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:

```tsx
// Demo.tsx
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} />}
    />
  );
}

// data.ts
export const employees = [
  { id: '1', firstName: 'Alice', lastName: 'Johnson', email: 'alice.johnson@example.com', department: 'Engineering', salary: 95000, startDate: '2021-03-15', status: 'active' },
  { id: '2', firstName: 'Bob', lastName: 'Smith', email: 'bob.smith@example.com', department: 'Marketing', salary: 72000, startDate: '2020-07-01', status: 'active' },
  { id: '3', firstName: 'Carol', lastName: 'Williams', email: 'carol.williams@example.com', department: 'Sales', salary: 68000, startDate: '2022-01-10', status: 'active' },
  { id: '4', firstName: 'David', lastName: 'Brown', email: 'david.brown@example.com', department: 'HR', salary: 65000, startDate: '2019-11-20', status: 'inactive' },
  { id: '5', firstName: 'Eva', lastName: 'Davis', email: 'eva.davis@example.com', department: 'Finance', salary: 88000, startDate: '2021-06-05', status: 'active' },
  { id: '6', firstName: 'Frank', lastName: 'Miller', email: 'frank.miller@example.com', department: 'Engineering', salary: 102000, startDate: '2018-09-12', status: 'active' },
  { id: '7', firstName: 'Grace', lastName: 'Wilson', email: 'grace.wilson@example.com', department: 'Marketing', salary: 76000, startDate: '2022-04-18', status: 'active' },
  { id: '8', firstName: 'Henry', lastName: 'Taylor', email: 'henry.taylor@example.com', department: 'Sales', salary: 71000, startDate: '2020-02-28', status: 'inactive' },
  { id: '9', firstName: 'Irene', lastName: 'Anderson', email: 'irene.anderson@example.com', department: 'Engineering', salary: 97000, startDate: '2021-08-30', status: 'active' },
  { id: '10', firstName: 'James', lastName: 'Thomas', email: 'james.thomas@example.com', department: 'Finance', salary: 91000, startDate: '2019-05-14', status: 'active' },
  { id: '11', firstName: 'Karen', lastName: 'Jackson', email: 'karen.jackson@example.com', department: 'HR', salary: 63000, startDate: '2023-01-09', status: 'active' },
  { id: '12', firstName: 'Leo', lastName: 'White', email: 'leo.white@example.com', department: 'Engineering', salary: 110000, startDate: '2017-12-01', status: 'active' },
  { id: '13', firstName: 'Mia', lastName: 'Harris', email: 'mia.harris@example.com', department: 'Sales', salary: 74000, startDate: '2022-07-22', status: 'active' },
  { id: '14', firstName: 'Nathan', lastName: 'Martin', email: 'nathan.martin@example.com', department: 'Marketing', salary: 69000, startDate: '2021-10-03', status: 'inactive' },
  { id: '15', firstName: 'Olivia', lastName: 'Garcia', email: 'olivia.garcia@example.com', department: 'Finance', salary: 85000, startDate: '2020-04-11', status: 'active' },
  { id: '16', firstName: 'Patrick', lastName: 'Martinez', email: 'patrick.martinez@example.com', department: 'Engineering', salary: 99000, startDate: '2019-08-25', status: 'active' },
  { id: '17', firstName: 'Quinn', lastName: 'Robinson', email: 'quinn.robinson@example.com', department: 'HR', salary: 67000, startDate: '2022-11-14', status: 'active' },
  { id: '18', firstName: 'Rachel', lastName: 'Clark', email: 'rachel.clark@example.com', department: 'Sales', salary: 73000, startDate: '2021-02-07', status: 'active' },
];
```


## 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](https://alpha.mantine.dev/llms/data-grid-customization.md#column-menu).

## 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:

```tsx
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`:

## Sort removal behavior

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

```tsx
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:

```tsx
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](https://alpha.mantine.dev/llms/data-grid-server-side.md) 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.
