addSortBy

addSortBy sorts table rows by column values.

Options

Information:
Options passed into 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

Information:
Options passed into column definitions.
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

Information:
Extensions to the view model. Read 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 addPagination and addVirtualScroll, so a sort always reorders the whole data set, not just the visible page.

Plugin State

Information:
State provided by addSortBy.
const vm = table.createViewModel(columns)
const { pluginStates } = vm
const { sortKeys } = pluginStates.sort
const vm = table.createViewModel(columns)
const { pluginStates } = vm
const { sortKeys } = pluginStates.sort

sortKeys: 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
KristyHarris27relationship6230
CamrenSchaden58relationship4985
HalMetz28complicated1022
MabelHowell56single5816
StephonKeebler38single7854
FrankBalistreri29single6100
OscarLedner35complicated499
DewayneRippin36relationship180
MajorJohns34relationship5598
MirandaJohnson48single6617
SedrickLittle28complicated9259
JammieShanahan47complicated1759
DemondGoodwin18relationship6329
CoralieLowe58relationship994
SergioO'Keefe56single323
FreidaBergstrom20relationship3967
LafayetteWilkinson39complicated2042
AmosQuigley55single7850
MozelleStanton39relationship720
MagnusHowell62single5792
DarnellMaggio60single6885
CaleStroman56single3557
NewtonBreitenberg38single2288
ErickJohns62single6439
OdessaPouros65single2479
JessicaGulgowski26complicated820
MustafaRatke60complicated9965
CaylaSkiles33single99100
ShemarNitzsche21relationship6235
JermeyGibson37single441