Skip to content

Sorting

Sorting is on by default. Click a column header (or its sort icon) to cycle ascending, descending and off. Hold Shift while clicking to sort by several columns at once.

<MaterialSvelteTable
options={{
columns,
data,
initialState: { sorting: [{ id: 'lastName', desc: false }] },
}}
/>
OptionWhat it does
enableSortingTurn sorting off for the table, or per column.
enableMultiSortAllow sorting by more than one column. On by default.
enableSortingRemovalAllow clicking a third time to clear the sort.
sortDescFirstStart with descending instead of ascending (also available per column).
sortFn (column)Pick a built in sort function, or pass your own.

Built in sort functions: alphanumeric, alphanumericCaseSensitive, text, textCaseSensitive, datetime, basic. By default MRT chooses one from the first value in the column.

{ accessorKey: 'name', header: 'Name', sortFn: 'textCaseSensitive' }
{
accessorKey: 'priority',
header: 'Priority',
sortFn: (rowA, rowB, columnId) =>
order.indexOf(rowA.getValue(columnId)) - order.indexOf(rowB.getValue(columnId)),
}

To refer to a custom sort function by name, register it with the sortFns option. Like filter functions, these are registered once, when the table is created.

Set manualSorting and put the sorting state in state with an onSortingChange handler. Fetch the sorted data yourself:

<script lang="ts">
let sorting = $state<MRT_SortingState>([]);
let data = $state<Person[]>([]);
$effect(() => {
fetchPeople({ sorting }).then((rows) => (data = rows));
});
</script>
<MaterialSvelteTable
options={{
columns,
get data() {
return data;
},
manualSorting: true,
onSortingChange: (updater) => {
sorting = typeof updater === 'function' ? updater(sorting) : updater;
},
state: {
get sorting() {
return sorting;
},
},
}}
/>

See Remote data for a complete example with sorting, filtering and pagination.