Sorting should be intuitive and fast.
Click behavior:
First click: Sort ascending
Second click: Sort descending
Third click: Remove sort (optional)
Implementation:
const sortedData = useMemo(() => {
if (!sorting) return data;
return [...data].sort((a, b) => {
const aVal = a[sorting.column];
const bVal = b[sorting.column];
const cmp = aVal < bVal ? -1 : aVal > bVal ? 1 : 0;
return sorting.direction === 'asc' ? cmp : -cmp;
});
}, [data, sorting]);
Multi-column sort: Support Shift+click for secondary sort.