Skip to main content

Column Sorting

The grid supports two mode of sorting

  • Single column sorting
  • Multi column sorting

The default is multi column sorting. To enable single column sorting, set the enableMultiColumnSort prop to false on sortOptions prop of the GridOptions interface.

Single column sorting

Live Editor
Result
Demo
Id
First Name
Last Name
Line 1
City
State
Country
Address
1
Brice
Rittwage
437 King Ave
Stroudsburgh
CA
USA
2
CHUN-FEN
Hukill
840 Gardner St
Grand Rapids
NY
USA
3
Takahiro
Aksu
171 Newark Blvd
Stroudsburgh
CA
USA
4
Corinthians
Orchard
247 West St
Stroudsburgh
NJ
USA
5
Karna
Arledge
226 Park Ave
Springfield
NJ
USA
6
Takehisa
Akmanligil
694 West St
Springfield
CA
USA
7
Ranjana
Anupoju
832 Newark Lane
Barrie
TX
USA
8
Erico
Riddell
779 Gardner Blvd
Barrie
NY
USA
|
466 item(s)

Notice that the sort number is not visible on the header of the column that is currently sorted. This is because we turned off multi column sorting. The top left sort icon is also not visible. Now, lets contrast this with what the default behavior is.

Multi column sorting

Live Editor
Result
Demo
Id
First Name
Last Name
Line 1
City
State
Country
Address
1
Brice
Rittwage
437 King Ave
Stroudsburgh
CA
USA
2
CHUN-FEN
Hukill
840 Gardner St
Grand Rapids
NY
USA
3
Takahiro
Aksu
171 Newark Blvd
Stroudsburgh
CA
USA
4
Corinthians
Orchard
247 West St
Stroudsburgh
NJ
USA
5
Karna
Arledge
226 Park Ave
Springfield
NJ
USA
6
Takehisa
Akmanligil
694 West St
Springfield
CA
USA
7
Ranjana
Anupoju
832 Newark Lane
Barrie
TX
USA
8
Erico
Riddell
779 Gardner Blvd
Barrie
NY
USA
|
466 item(s)

Notice that the sort number is visible on the header of the column that is currently sorted. This is because we turned on multi column sorting. The top left sort icon is also visible. Clicking on the sort icon in the top left will add the column to the sort instead of clearing the sort and sorting on the column that was clicked. Additionally, clicking with the control or meta key held down will also add the column to the sort.

Initial sort

For the initial sort, you can set the initialSort prop on the sortOptions prop of the GridOptions interface. This is what this looks like

        sortOptions: ({
initialSort: new Map([["id",
{
sortColumn: "id",
isAscending: true,
}]]
)
}),

As you can see, the initialSort prop is a Map of SortColumn objects. The key of the Map is the sortColumn prop of the SortColumn object. The value of the Map is the SortColumn object itself. The sortColumn prop is the name of the column to sort on. The isAscending prop is a boolean that determines whether the sort is ascending or descending.

To specify mutiple values,

        sortOptions: ({
initialSort: new Map([["id",
{
sortColumn: "id",
isAscending: true,
sortIndex: 1,
}],
["firstName",
{
sortColumn: "firstName",
isAscending: false,
sortIndex: 2,
}]]
)
}),

Lets look at an example of this in action

Live Editor
Result
Demo
Id
First Name
Last Name
1
Brice
Rittwage
2
CHUN-FEN
Hukill
3
Takahiro
Aksu
4
Corinthians
Orchard
5
Karna
Arledge
6
Takehisa
Akmanligil
7
Ranjana
Anupoju
8
Erico
Riddell
9
Llora
Lovig
|
466 item(s)

And here is what it looks like for multiple columns

Live Editor
Result
Demo
Id
First Name
Last Name
1
Brice
Rittwage
2
CHUN-FEN
Hukill
3
Takahiro
Aksu
4
Corinthians
Orchard
5
Karna
Arledge
6
Takehisa
Akmanligil
7
Ranjana
Anupoju
8
Erico
Riddell
9
Llora
Lovig
|
466 item(s)