Sorting

Add sortable: true to any column to enable client-side sorting on that column. Click a header to sort; click again to reverse direction. Columns without sortable are not interactive and are excluded from the tab order.

Sorting playground

Toggle the default sort column, direction, a custom icon, and per-column sort functions. The onSort readout shows the callback firing on every sort change.

Default column:
Default direction:
onSort → (click a column header to see the callback fire)
Name
Department
Salary
Hired
ID
Aria Chen
Engineering
$155,000
2019-03-12
1
Casey Park
Design
$109,000
2022-11-19
8
Jordan Ellis
Analytics
$143,000
2018-11-30
4
Marcus Webb
Product
$132,000
2020-07-01
2
Morgan Lee
Engineering
$162,000
2017-09-05
7
Priya Kapoor
Design
$118,000
2021-01-15
3
Sam Rivera
Engineering
$128,000
2022-04-22
5
Taylor Brooks
Sales
$97,000
2023-02-08
6
Étienne Fontaine
Sales
$108,000
2023-06-01
9

Default sort

defaultSortFieldId sets which column is sorted on first render. Its value must match the id on the column definition. defaultSortAsc controls the initial direction; it defaults to true (ascending).

// Sort by salary descending on load
<DataTable
  columns={columns}
  data={data}
  defaultSortFieldId="salary"
  defaultSortAsc={false}
/>

Listening to sort events

onSort fires on every sort change, whether triggered by clicking a header or by a defaultSortFieldId change. It receives the sorted column, the new direction, and the fully sorted row array.

import { SortOrder } from 'react-data-table-component';

<DataTable
  columns={columns}
  data={data}
  onSort={(column, direction, sortedRows) => {
    console.log('sorted by', column.id, direction);
    // sortedRows is the full sorted array after the sort is applied
  }}
/>

Custom sort function: per column

Override the sort comparator for a specific column with column.sortFunction. It receives two row objects and returns a number, just like Array.prototype.sort. Use this for locale-aware strings, currency values, or any non-default ordering.

const columns: TableColumn<Employee>[] = [
  {
    id: 'name',
    name: 'Name',
    selector: r => r.name,
    sortable: true,
    sortFunction: (a, b) => a.name.localeCompare(b.name, undefined, { sensitivity: 'base' }),
  },
  {
    id: 'salary',
    name: 'Salary',
    selector: r => r.salary,
    sortable: true,
    sortFunction: (a, b) => a.salary - b.salary,
  },
];

Custom sort function: table level

The table-level sortFunction prop replaces the entire sort algorithm for every column. It receives the full row array, the selector function for the active column, and the sort direction. Return the sorted array. This is less common than per-column sortFunction but useful when you need a single unified sort strategy across all columns.

import { SortOrder, type SortFunction } from 'react-data-table-component';

const customSort: SortFunction<Employee> = (rows, selector, direction) => {
  return [...rows].sort((a, b) => {
    const aVal = selector(a);
    const bVal = selector(b);
    if (aVal === bVal) return 0;
    const cmp = aVal > bVal ? 1 : -1;
    return direction === SortOrder.ASC ? cmp : -cmp;
  });
};

<DataTable columns={columns} data={data} sortFunction={customSort} />

Priority ordering: sort certain rows first

Sometimes you want specific rows to float to the top regardless of what column is sorted. There are two distinct patterns: sorting a column by a custom value order (e.g. status priority), and anchoring specific rows by identity regardless of sort. The latter is covered in depth on the Row pinning page.

Priority sort patterns

Two modes: sort a Status column by custom priority order (Active → On Leave → Terminated), and break ties within the same primary value using a secondary salary sort.

Sort Status by a custom priority: Active → On Leave → Terminated. Click the Status header.

Name
Department
Status
Salary
Aria Chen
Engineering
Active
$155,000
Jordan Ellis
Analytics
Active
$143,000
Taylor Brooks
Sales
Active
$97,000
Casey Park
Design
Active
$109,000
Alex Torres
Analytics
Active
$151,000
Priya Kapoor
Design
On Leave
$118,000
Morgan Lee
Engineering
On Leave
$162,000
Marcus Webb
Product
Terminated
$132,000
Sam Rivera
Engineering
Terminated
$128,000
Jamie Okonkwo
Sales
Terminated
$88,000

Sorting null / empty values last

By default, empty strings and null sort ahead of real values alphabetically. Push them to the bottom with a guard in your comparator:

const nullsLastSort: SortFunction<Employee> = (rows, selector, direction) =>
  [...rows].sort((a, b) => {
    const av = selector(a);
    const bv = selector(b);
    // Empty / null → always sink to bottom regardless of direction
    const aEmpty = av === null || av === undefined || av === '';
    const bEmpty = bv === null || bv === undefined || bv === '';
    if (aEmpty && bEmpty) return 0;
    if (aEmpty) return 1;
    if (bEmpty) return -1;
    const cmp = av > bv ? 1 : -1;
    return direction === SortOrder.ASC ? cmp : -cmp;
  });

<DataTable columns={columns} data={data} sortFunction={nullsLastSort} />

Custom sort icon

Pass any React node to sortIcon to replace the default chevron. The icon is rendered inside a span that receives rdt_sortIconActive / rdt_sortIconInactive and rdt_sortIconAsc class names. Use these to style direction and active state with CSS.

// A simple triangle that flips via CSS
const SortIcon = () => (
  <svg width="10" height="10" viewBox="0 0 10 10" fill="currentColor" aria-hidden="true">
    <path d="M5 1l4 8H1z" />
  </svg>
);

// Flip the icon for descending with CSS:
// .rdt_sortIcon:not(.rdt_sortIconAsc) svg { transform: rotate(180deg); }

<DataTable columns={columns} data={data} sortIcon={<SortIcon />} />

Server-side sorting

Set sortServer to stop the table from sorting rows locally. DataTable still updates its visual sort indicator and fires onSort. Your handler is responsible for fetching or reordering data and passing the updated array back via data.

When a column maps to a different backend field name, set sortField on the column. The value is passed to onSort via the column object so you can forward it directly to your query.

Server-side sorting

Click any column header to trigger a fake server fetch (250 ms latency). The table shows a loading indicator while the sorted data arrives.

onSort → (click a sortable column header)
Name
Department
Salary
Hired
Aria Chen
Engineering
$155,000
2019-03-12
Marcus Webb
Product
$132,000
2020-07-01
Priya Kapoor
Design
$118,000
2021-01-15
Jordan Ellis
Analytics
$143,000
2018-11-30
Sam Rivera
Engineering
$128,000
2022-04-22
Taylor Brooks
Sales
$97,000
2023-02-08
Morgan Lee
Engineering
$162,000
2017-09-05
Casey Park
Design
$109,000
2022-11-19
Drew Okafor
HR
$88,000
2023-08-14
Riley Zhang
Analytics
$136,000
2020-12-03

Combined: server-side sorting and pagination

When using both sortServer and paginationServer, the three callbacks are mutually exclusive. Only one fires per interaction. Sorting resets the page to 1 automatically and onChangePage is suppressed, so onSort is your only fetch point for a new sort. Always fetch from page 1 there and update your local page state to 1 to stay in sync. See the pagination docs for the complete callback contract.

Server-side sort + pagination

150-row simulated dataset. Sort any column, then page through the results. Sorting resets to page 1 automatically.

sort: "name" asc · page: 1 · perPage: 10 · total: 0
Name
Department
Salary
Status

Prop reference

Prop Type Default Description
defaultSortFieldId string | number - Column id to sort by on first render.
defaultSortAsc boolean true Initial sort direction. false = descending.
onSort (column, direction, sortedRows) => void - Called on every sort change with the active column, direction, and sorted rows.
sortServer boolean false Disable client-side sorting. Use with onSort to sort remotely.
sortFunction SortFunction<T> - Table-level sort algorithm. Replaces the default sort for all columns.
sortIcon ReactNode - Custom icon rendered inside sortable column headers.
column.sortable boolean false Enable sorting on this column.
column.sortFunction (a, b) => number - Per-column sort comparator. Takes priority over the table-level sortFunction.
column.sortField string - Backend field name passed to onSort when it differs from column.id.