# Sorting

## Content

<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">The Grid control supports the three-state sorting through the </span>**CollectionView**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> class. By default, clicking on any column header sorts the data based on that column. Users can toggle between Ascending, Descending and None sorting states by clicking the column header.</span>
<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">By default data isn the Grid will be sorted only in ascending and descending order. To enable three-state sort, the sortOrder property needs to be set to AscDescNone or DescAscNone.</span>
![sort](https://cdn.mescius.io/document-site-files/images/3c7113e2-10b3-45ed-8f3b-9fb1e0af2b74/sort.d2e945.gif?width=400)

> type=note
> **Note:**
> To remove the sorting option from a column, click the column header with Ctrl key. To remove all sorts, click the column header in combination with Shift + Control keys.

A user can control the behavior of the sorting in a **Grid** control at grid and column levels by using the following enum values of the **sortOrder** property of the **FlexGrid** class:

* **AscDesc** : It allows sorting of data from ascending to descending order first.
* **DescAsc**: It allows sorting of data from descending to ascending order first.
* **AscDescNone**: It restricts the sorting of data from ascending to descending order.
* **DescAscNone** : It restricts the sorting of data from descending to ascending order.

> type=note
> **Note:** The precedence is given to the sort order on columns when sortOrder property is set on both grid and column.

<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Additionally, to customize sorting behavior use the </span>**showSort**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> and </span>**allowSorting**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> properties of the FlexGrid class. The </span>**showSort**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> property determines whether the grid displays sort indicators in the column headers. The </span>**allowSorting**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> property allows users to prevent sorting in the entire grid control or specific columns. The default value for both the properties is true. The Grid contains the events ‘</span>**sortingColumn**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">’ and ‘</span>**sortedColumn**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">’ which get fired when sorting is applied by clicking on the column headers in the Grid or in the Filter dialog.</span>

> type=info
> **Limitation**:
> In FlexSheet, the **sortingColumn** and **sortedColumn** events are not triggered when sorting is performed using the **SortManager**.

<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">The following example code is used to enable the sorting the behavior in the FlexGrid at grid and column levels. Here the sortOrder: wjGrid.SortOrder.AscDesc ensures that when the user clicks on the column headers (Country, Sales, Expenses), the rows will toggle between ascending and descending order depending on the number of clicks.</span>

```auto
var theGrid = new wjGrid.FlexGrid('#theGrid', {
        deferResizing: true,
        sortOrder: wjGrid.SortOrder.AscDesc,
        autoGenerateColumns: false,
        columns: [
            { binding: 'id', header: 'ID', width: 60, allowSorting: false },
            { binding: 'country', header: 'Country', sortOrder: wjGrid.SortOrder.AscDescNone },
            { binding: 'sales', header: 'Sales', sortOrder: 'DescAsc' },
            { binding: 'expenses', header: 'Expenses' }
        ],
        itemsSource: data
    }); 
```

## On Demand Sorting

The Grid control uses the **CollectionView** class to manage data operations like sorting, filtering, grouping, paging, and change tracking. By default, the CollectionView automatically refreshes and re-sorts the data after an edit. However, users can modify this behavior by setting the **refreshOnEdit** property of the FlexGrid class to false.
When **refreshOnEdit** is set to false, the **CollectionView** will only re-sort the data when triggered by specific actions, such as clicking a column header. This gives users greater control over sorting, improving performance and user experience, especially with large datasets or frequent updates.
The following example code displays how to control the sorting behavior in a Grid control during editing:

```auto
function init() {
    // create the grid with on-demand sorting
    var theGrid = new FlexGrid('#theGrid', {
        alternatingRowStep: 0,
        itemsSource: new CollectionView(getData(), {
        refreshOnEdit: false // on-demand sorting and filtering
        })
    });
```

## Multicolumn Sorting

To enable multi-column sorting, set the **allowSorting** property to **AllowSorting.MultiColumn**. Setting this property allows users to sort multiple columns simultaneously. When sort on multiple columns, the grid displays the sort index in the column header next to the sort direction indicator.

> type=note
> **Note**: 
> The default value for **allowSorting** property is **AllowSorting.SingleColumn**.

![multicolumnsorting](https://cdn.mescius.io/document-site-files/images/3c7113e2-10b3-45ed-8f3b-9fb1e0af2b74/multicolumnsorting.7ba51a.png?width=450)
<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;">Users can customize the appearance of the sort index by styling the </span>**wj-sort-index**<span style="color: rgb(41, 42, 46); font-family: Atlassian Sans, ui-sans-serif, -apple-system, BlinkMacSystemFont, Segoe UI, Ubuntu, system-ui, Helvetica Neue, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; white-space: pre-wrap; background-color: rgb(255, 255, 255); text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; display: inline !important; float: none;"> class. The following example code shows how to enable multicolumn sorting in a Grid control.</span>

```auto
function init() {
    new FlexGrid('#theGrid', {
        itemsSource: getData(200),
        allowSorting: AllowSorting.MultiColumn
    });
}
```