Sorting
Tapping a header cycles a column through ascending, descending, and unsorted, and returns to the first page.
Basic usage
sorting.tsx
import { useState } from 'react';
import { MaterialIcons } from '@expo/vector-icons';
import {
DataGrid,
type ColumnDef,
type SortModel,
} from 'expo-flash-datagrid';
import { users, type User } from './data';
const STATUS_ORDER = ['new', 'active', 'paused'];
const columns: ColumnDef<User>[] = [
{ field: 'name', headerName: 'Name', flex: 1, minWidth: 160 },
{
field: 'status',
headerName: 'Status',
width: 120,
sortComparator: (a, b) =>
STATUS_ORDER.indexOf(String(a)) - STATUS_ORDER.indexOf(String(b)),
},
{ field: 'email', headerName: 'Email', width: 200, sortable: false },
];
export function SortingScreen() {
const [sortModel, setSortModel] = useState<SortModel>([
{ field: 'name', sort: 'asc' },
]);
return (
<DataGrid<User>
mode="client"
rows={users}
columns={columns}
sortModel={sortModel}
onSortModelChange={setSortModel}
enableMultiSort
showUnsortedSortIcon
sortIcons={{
asc: <MaterialIcons name="arrow-upward" size={14} />,
desc: <MaterialIcons name="arrow-downward" size={14} />,
none: <MaterialIcons name="unfold-more" size={14} />,
}}
style={{ flex: 1 }}
/>
);
}
Options
| Prop or field | Type | Default | Description |
|---|---|---|---|
sortModel | SortModel | [] | Controlled sort order. |
onSortModelChange | (next: SortModel) => void | None | Called when the sort changes. |
enableMultiSort | boolean | false | Keep earlier columns sorted when another is tapped. |
sortIcons | DataGridSortIcons | Built-in icons | Custom asc, desc, and none icons. |
showUnsortedSortIcon | boolean | false | Show the none icon on sortable, unsorted columns. |
column.sortable | boolean | true | Set false to disable sorting for a column. |
column.sortComparator | (a, b, rowA, rowB) => number | By column type | Custom comparison. |
Actions columns are never sortable.
Client vs server
In client mode the grid sorts rows. In server mode it only updates sortModel, and your API applies the order. sortComparator has no effect there.