Multi-sort without modifier keys
Add, replace, reorder and remove sort rules with the same controls on touch and keyboard. The host applies the query.
Keyboard
- Open Sort and use Add sort to create a rule. New rules receive focus.
- Choose a column and direction, then use Raise or Lower sort priority to change precedence.
- Remove sort returns focus to the remaining rule, or Add sort when none remain.
- Column headings still support Shift-select as a shortcut.
States covered
- controlled multi-sort
- touch sort editor
- keyboard priorities
- host sorting
- non-sortable identity
- uncontrolled compact view
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-data-grid-priority-sorting.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useState } from 'react';
import { Button, DataGrid, createDataGridViewState, type DataColumn, type DataSort } from '@bld8/ui';
type Release = { id: string; revision: number; title: string; phase: string; priority: number };
const columns: DataColumn<Release>[] = [
{ key: 'id', label: 'Release', type: 'text', sortable: false, hideable: false, minWidth: 104, width: 104 },
{ key: 'title', label: 'Release summary', type: 'text', minWidth: 210 },
{ key: 'phase', label: 'Release phase', type: 'text', minWidth: 150 },
{ key: 'priority', label: 'Release priority', type: 'number', minWidth: 140 }
];
const rows: Release[] = [
{ id: 'REL-1', revision: 1, title: 'Calendar keyboard checks', phase: 'Review', priority: 2 },
{ id: 'REL-2', revision: 1, title: 'Upload retry messages', phase: 'Draft', priority: 3 },
{ id: 'REL-3', revision: 1, title: 'Chart legend contrast', phase: 'Review', priority: 3 },
{ id: 'REL-4', revision: 1, title: 'Toolbar spacing', phase: 'Ready', priority: 1 }
];
const compactView = { ...createDataGridViewState(columns), density: 'compact' as const };
export default function OrderedGridSortExample() {
const [sorts, setSorts] = useState<DataSort[]>([{ key: 'priority', descending: true }, { key: 'title', descending: false }]);
const orderedRows = [...rows].sort((left, right) => {
for (const sort of sorts) {
const a = left[sort.key as keyof Release], b = right[sort.key as keyof Release];
const result = typeof a === 'number' && typeof b === 'number' ? a - b : String(a).localeCompare(String(b));
if (result) return sort.descending ? -result : result;
}
return 0;
});
return <div style={{ display: 'grid', gap: 16, minWidth: 0 }}>
<DataGrid label="Sorted release queue" rows={orderedRows} columns={columns} sorts={sorts} onSortsChange={setSorts} defaultView={compactView} />
<div><Button variant="ghost" size="sm" onClick={() => setSorts([])}>Reset example sorting</Button></div>
<output aria-label="Active sort rules" style={{ overflowWrap: 'anywhere' }}>{sorts.length ? sorts.map((sort, index) => (index + 1) + '. ' + sort.key + ' ' + (sort.descending ? 'descending' : 'ascending')).join('; ') : 'No sorting'}</output>
</div>;
}