Theme

Examples › Sorting

Crist and Sons3387 Blick TurnpikeAttleboroWV
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Johnston LLC230 Julie LakeHartfordKY
Kub and Sons8699 Upton FordsEast ProvidenceIN
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
Nicolas Group09622 Marcel PlaceHighlandOR
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Schmidt and Sons286 Leif LockColliervilleAL
Sipes Inc280 Rigoberto DivideTwin FallsMT
Welch - Tremblay31622 Isobel FallOlatheAR
No records
In order to enable sorting, you’ll have to:
  • add a sortable: true property to each sorting candidate column;
  • add a sortStatus property to the DataTable component, pointing to an object describing the current sort status;
  • add a handler called onSortStatusChange to the DataTable to perform the required action when a sortable column header is clicked.
import sortBy from 'lodash/sortBy';
import { DataTable, DataTableSortStatus } from 'mantine-datatable';
import { useEffect, useState } from 'react';
import { companies, type Company } from '~/data';
export default function SortingExample() {
const [sortStatus, setSortStatus] = useState<DataTableSortStatus>({ columnAccessor: 'name', direction: 'asc' });
const [records, setRecords] = useState(sortBy(companies, 'name'));
useEffect(() => {
const data = sortBy(companies, sortStatus.columnAccessor) as Company[];
setRecords(sortStatus.direction === 'desc' ? data.reverse() : data);
}, [sortStatus]);
return (
<DataTable
withBorder
withColumnBorders
records={records}
columns={[
{ accessor: 'name', width: '40%', sortable: true },
{ accessor: 'streetAddress', width: '60%' },
{ accessor: 'city', width: 160, sortable: true },
{ accessor: 'state', sortable: true },
]}
sortStatus={sortStatus}
onSortStatusChange={setSortStatus}
/>
);
}

Custom sort icons

If you want to use custom icons for sorting, you can pass them to the sortIcons prop:
import { IconChevronUp, IconSelector } from '@tabler/icons-react';
import sortBy from 'lodash/sortBy';
import { DataTable, DataTableSortStatus } from 'mantine-datatable';
import { useEffect, useState } from 'react';
import { companies, type Company } from '~/data';
export default function SortingExampleCustomIcons() {
const [sortStatus, setSortStatus] = useState<DataTableSortStatus>({ columnAccessor: 'name', direction: 'asc' });
const [records, setRecords] = useState(sortBy(companies, 'name'));
useEffect(() => {
const data = sortBy(companies, sortStatus.columnAccessor) as Company[];
setRecords(sortStatus.direction === 'desc' ? data.reverse() : data);
}, [sortStatus]);
return (
<DataTable
withBorder
withColumnBorders
records={records}
columns={[
{ accessor: 'name', width: '40%', sortable: true },
{ accessor: 'streetAddress', width: '60%' },
{ accessor: 'city', width: 160, sortable: true },
{ accessor: 'state', sortable: true },
]}
sortStatus={sortStatus}
onSortStatusChange={setSortStatus}
sortIcons={{
sorted: <IconChevronUp size={14} />,
unsorted: <IconSelector size={14} />,
}}
/>
);
}
Crist and Sons3387 Blick TurnpikeAttleboroWV
Jakubowski - Rolfson191 O'Connell GreensSan RafaelMA
Johnston LLC230 Julie LakeHartfordKY
Kub and Sons8699 Upton FordsEast ProvidenceIN
Mueller, Hodkiewicz and Beahan21751 Elisa VillageGrand PrairieWA
Nicolas Group09622 Marcel PlaceHighlandOR
Runolfsdottir - Cummerata102 Konopelski GreensMissouri CityKY
Schmidt and Sons286 Leif LockColliervilleAL
Sipes Inc280 Rigoberto DivideTwin FallsMT
Welch - Tremblay31622 Isobel FallOlatheAR
No records

Mantine DataTable is trusted by

MIT LicenseSponsor the author
Built by Ionut-Cristian Florescu and these awesome people.
Please sponsor the project if you find it useful.
GitHub StarsNPM Downloads