View as Markdown

Sorting

Sorting

Header sorting, multi-column sort, initial sort, custom comparators and sort methods.

Clicking a header, or pressing Enter on a focused header, cycles ascending, descending and unsorted; sorting.order replaces that cycle, so order: ['asc', 'desc'] never returns to unsorted. Ctrl/⌘+click adds the column to a multi-column sort; headers show the direction and the sort order badge and expose aria-sort. A grid can start sorted with sorting.sortBy: [{ field, direction: 'asc' }], a column opts out with sortable: false and a column can supply its own sortComparator(a, b, rowA, rowB).

<DataGrid
  data={orders}
  columns={columns}
  sorting={{ multiple: true, sortBy: [{ field: 'country', direction: 'asc' }] }}
  onSortChange={(event) => console.log(event.sortBy)}
/>

api.sortColumn(field, direction, { multiple }) sorts a column, api.removeSort(field) removes one column from the sort and api.clearSorting() clears it. onBeforeSortChange can cancel a change with preventDefault(); onSortChange reports the sortBy, the previousSortBy and the field that triggered it. sorting.sortBy is part of the server render, so a grid renders already sorted.

sorting.multiple turns multi-column sorting on; it defaults to false, where Ctrl/⌘+click replaces the sort like a plain click. With columnMenu, the built-in 'sortAscending', 'sortDescending' and 'clearSort' items sort the column the menu was opened on; the item for the current direction is disabled.

By default only a sorted header shows its direction arrow. sorting.indicators: 'always' gives every sortable header an up and down arrow pair, the active direction at full strength, so the sortable columns are visible before the first click:

<DataGrid data={orders} columns={columns} sorting={{ indicators: 'always' }} />

On a focused header, Enter sorts, Ctrl/⌘+Enter adds the column to a multi-column sort and Shift+Enter removes it from the sort.

Touch multi-sort

On a touch screen there is no Ctrl key, so with sorting.multiple on, a header tap that sorts also shows a small popup by that header with a Multi-sort toggle button. While the toggle is pressed (aria-pressed="true"), each header tap adds its column to the sort, or cycles it in place when it is already sorted, exactly like Ctrl/⌘+click; while it is not pressed a tap replaces the sort. The popup follows the last tapped header and stays open across header taps. A press anywhere else (a body cell, outside the grid) or Escape hides it and turns the toggle off, so the next tap starts a new single-column sort. There is no timeout. Mouse clicks and the keyboard never show the popup; with sorting.multiple off, taps sort without it. The label is the touchMultiSort key of localeText. The popup code is part of the lazy overlay chunk and loads on the first touch sort.

Stability

The sort is stable: rows with equal keys keep their order in the data, so sorting the demo by Country lists each country's orders by order number. A custom sortComparator that returns 0 keeps the same guarantee.

The sort runs in the core engine and is incremental, so re-sorting 100,000 rows does not re-render the grid beyond the visible cells.

Sorting

Open demo

Header click cycles ascending, descending and unsorted; multi-column sort (switchable with sorting.multiple) with order badges and aria-sort; a Multi-sort toggle popup on touch header taps; sorting from the column menu; a stable sort; an initial sort; a custom comparator; a non-sortable column; sort methods.

Order
Customer
Country
Priority (custom order)
Amount
Placed
Note (not sortable)
ORD-14838
Goran Petrov
Malaysia
High
$584.10
Dec 28, 2026
ORD-16672
Farah Aziz
Germany
High
$319.20
Dec 28, 2026
Leave the parcel with the building reception if nobody answers the door.
ORD-12421
Ines Duarte
Malaysia
High
$119.70
Dec 28, 2026
ORD-11692
Bao Nguyen
Japan
High
$13.30
Dec 28, 2026
Gift wrap
ORD-17377
Hana Sato
Portugal
High
$516.00
Dec 28, 2026
ORD-11955
Jun Park
France
High
$130.05
Dec 27, 2026
Gift wrap
ORD-19058
Farah Aziz
Germany
High
$399.00
Dec 27, 2026
ORD-18254
Bao Nguyen
France
High
$548.25
Dec 27, 2026
Leave the parcel with the building reception if nobody answers the door.
ORD-11475
Chen Wei
Germany
High
$83.25
Dec 27, 2026
ORD-16993
Hana Sato
Germany
High
$700.92
Dec 27, 2026
ORD-12303
Goran Petrov
Japan
High
$980.40
Dec 27, 2026
ORD-10531
Ines Duarte
Portugal
High
$26.60
Dec 27, 2026
ORD-17146
Aiko Tan
France
High
$464.40
Dec 27, 2026
Gift wrap
ORD-13223
Chen Wei
Japan
High
$87.88
Dec 27, 2026
ORD-17115
Hana Sato
Malaysia
High
$58.28
Dec 26, 2026
Leave the parcel with the building reception if nobody answers the door.
ORD-10325
Elif Kaya
Japan
High
$612.75
Dec 26, 2026
ORD-19380
Hana Sato
Germany
High
$42.00
Dec 26, 2026
Gift wrap
ORD-17134
Goran Petrov
Malaysia
High
$129.50
Dec 26, 2026
ORD-11896
Ines Duarte
Germany
High
$145.35
Dec 26, 2026
ORD-13291
Farah Aziz
Portugal
High
$39.31
Dec 26, 2026
Call before delivery

Click a header to cycle ascending, descending and unsorted; Ctrl/⌘+click adds a column to the sort while multi-column sort is on. On a touch screen, a header tap shows a Multi-sort toggle: while it is pressed, taps add columns. The column menu sorts too.

API reference

The DataGridSorting, DataGridSortChangeEvent and DataGridApi reference pages list every option and method.