Sorting

React Bootstrap Data Grid implements simple sorting of data, one column at a time, in ascending or descending order. The user can change the column being sorted and the sort order by clicking on a column header.

When a user clicks on a column header, the sort order changes between ascending, descending, and none. When the sort order is none, the rows in the table will appear in original order of the array in which data was passed to the Grid component.

Note that sorting is done on the original data, before pagination and before formatting functions are applied. In particular, note that numbers will be sorted by the value of the number rather than lexicographically. Similarly, dates and datetimes will be sorted by the number of milliseconds after the epoch time as opposed to any time of string representation of the Date object.

Like other features of React Bootstrap Data Grid, state storage and updates for the sorting feature is done in a fully controlled manner. That is, the state and updater function are defined in a parent component and passed down to the Grid component.

To allow a user to sort a Grid by a particular column, set the sortable property to true in the column definition (ColDef) for the column.

In addition, setting the sortModel property on the Grid component is required to enable sorting.

Type Definitions

See below for specifications for the sortModel property and related types.

TableSortModel

Type specification for the TableSortModel type, a prop to the Grid component
Property nameType definitionRequired/OptionalDescription
sortColDefSortColDef | nullRequiredThe current setting for sorting for the Grid
setSortColDef(sortColDef: SortColDef | null) => voidRequired

Callback function to set the state of sortColDef in response to user input

SortColDef

Type specification for SortColDef, which is part of TableSortModel
Property nameType definitionRequired/OptionalDescription
namestringRequiredThe name of the column to sort
order"asc" | "desc"RequiredThe sort order, ascending or descending

Example

Live Demo

Name(sorted ascending)Up arrow indicating that the column is being sorted in an ascending mannerAverage DamageDamage TypeWeight (lb)Price
Battleaxe5.5Slashing3.650
Flail4.5Bludgeoning250
Greataxe6.5Slashing6.365
Greatsword7Slashing5.465
Longsword5.5Slashing2.740
Maul7Bludgeoning940
Morningstar4.5Piercing3.650
Rapier4.5Piercing1.880
Scimitar4.5Slashing2.730
War Pick4.5Piercing1.840
Warhammer5.5Bludgeoning5.450