addSortBy
addSortBy sorts table rows by column values.
Options
addSortBy.const table = createTable(data, {
sort: addSortBy({ ... }),
});const table = createTable(data, {
sort: addSortBy({ ... }),
});initialSortKeys?: SortKey[]
Sets the initial sort keys.
Defaults to [].
disableMultiSort?: boolean
Disables multi-sorting for the table.
Defaults to false.
isMultiSortEvent?: (event: Event) => boolean
Allows overriding the default multi-sort behavior.
Takes an Event and returns whether the action triggers a multi-sort.
Defaults to multi-sort on shift-click.
toggleOrder?: ()[]
Allows customization of the toggling order. This cannot contain duplicate values. Set this to ['asc', 'desc'] to disable toggling to an unsorted column.
Defaults to ['asc', 'desc', undefined].
serverSide?: boolean
If true, the sort plugin will have no effect on the rows of the table. Instead, you can control sorting by updating the table’s data. The plugin’s state can be used as variables in your data-fetching query to get sorted data from the server directly.
Defaults to false.
Column Options
const columns = table.createColumns([
table.column({
header: 'Name',
accessor: 'name',
plugins: {
sort: { ... },
},
}),
]);const columns = table.createColumns([
table.column({
header: 'Name',
accessor: 'name',
plugins: {
sort: { ... },
},
}),
]);disable?: boolean
Disables sorting on the column.
getSortValue?: (value) => string | number | (string | number)[]
Returns the value to sort by, for cell values that are not directly comparable.
compareFn?: (left, right) => number
A custom comparison for the column.
invert?: boolean
Inverts the sort order of the column.
Prop Set
current.props on table components. The lists below are generated from the plugin's TypeScript prop-set type, so they match the published package exactly.HeaderCell props cell.current.props.sort
order: "asc" | "desc" | undefined- Current sort order for this column.
toggle: (event: Event) => void- Function to toggle sorting on this column.
clear: () => void- Function to clear sorting on this column.
disabled: boolean- Whether sorting is disabled for this column.
BodyCell props cell.current.props.sort
order: "asc" | "desc" | undefined- Current sort order for this column.
Interactions
- With
addSubRows, sub-rows are sorted within their parent; parents are ordered among themselves and children among their siblings. - Sorting runs before
addPaginationandaddVirtualScroll, so a sort always reorders the whole data set, not just the visible page.
Plugin State
addSortBy.const vm = table.createViewModel(columns)
const { pluginStates } = vm
const { sortKeys } = pluginStates.sortconst vm = table.createViewModel(columns)
const { pluginStates } = vm
const { sortKeys } = pluginStates.sortsortKeys: SortKeys
The active sort keys, a Box<SortKey[]> with two helpers. Read or assign sortKeys.current; every write assigns a new array.
sortKeys.current // [{ id: 'age', order: 'desc' }]
sortKeys.current = [{ id: 'name', order: 'asc' }]
sortKeys.toggleId('age') // advance the column through the toggle order
sortKeys.toggleId('age', { multiSort: false }) // replace the other keys
sortKeys.clearId('name')sortKeys.current // [{ id: 'age', order: 'desc' }]
sortKeys.current = [{ id: 'name', order: 'asc' }]
sortKeys.toggleId('age') // advance the column through the toggle order
sortKeys.toggleId('age', { multiSort: false }) // replace the other keys
sortKeys.clearId('name')createSortKeys(initialKeys) builds a standalone SortKeys (it replaces the 6.x createSortKeysStore).
preSortedRows: ReadonlyBox<BodyRow[]>
The rows before sorting was applied.
Examples
sortKeys.current = []
| Name | Info | ||||
|---|---|---|---|---|---|
| First Name | Last Name | Age | Status | Visits | Profile Progress |
| Kristy | Harris | 27 | relationship | 62 | 30 |
| Camren | Schaden | 58 | relationship | 49 | 85 |
| Hal | Metz | 28 | complicated | 10 | 22 |
| Mabel | Howell | 56 | single | 58 | 16 |
| Stephon | Keebler | 38 | single | 78 | 54 |
| Frank | Balistreri | 29 | single | 6 | 100 |
| Oscar | Ledner | 35 | complicated | 4 | 99 |
| Dewayne | Rippin | 36 | relationship | 1 | 80 |
| Major | Johns | 34 | relationship | 55 | 98 |
| Miranda | Johnson | 48 | single | 66 | 17 |
| Sedrick | Little | 28 | complicated | 92 | 59 |
| Jammie | Shanahan | 47 | complicated | 17 | 59 |
| Demond | Goodwin | 18 | relationship | 63 | 29 |
| Coralie | Lowe | 58 | relationship | 99 | 4 |
| Sergio | O'Keefe | 56 | single | 3 | 23 |
| Freida | Bergstrom | 20 | relationship | 39 | 67 |
| Lafayette | Wilkinson | 39 | complicated | 20 | 42 |
| Amos | Quigley | 55 | single | 78 | 50 |
| Mozelle | Stanton | 39 | relationship | 72 | 0 |
| Magnus | Howell | 62 | single | 57 | 92 |
| Darnell | Maggio | 60 | single | 68 | 85 |
| Cale | Stroman | 56 | single | 35 | 57 |
| Newton | Breitenberg | 38 | single | 22 | 88 |
| Erick | Johns | 62 | single | 64 | 39 |
| Odessa | Pouros | 65 | single | 24 | 79 |
| Jessica | Gulgowski | 26 | complicated | 8 | 20 |
| Mustafa | Ratke | 60 | complicated | 99 | 65 |
| Cayla | Skiles | 33 | single | 99 | 100 |
| Shemar | Nitzsche | 21 | relationship | 62 | 35 |
| Jermey | Gibson | 37 | single | 44 | 1 |