Grid Sorting Overview

In Ignite UI for Angular Grid, data sorting is enabled on a per-column level, meaning that the igx-grid can have a mix of sortable and non-sortable columns. Performing angular sort actions enables you to change the display order of the records based on specified criteria.

Demo


This is done via the sortable input. With the Grid sorting, you can also set the sortingIgnoreCase property to perform case sensitive sorting:

<igx-column field="ProductName" header="Product Name" [dataType]="'string'" sortable="true"></igx-column>

Sorting through the API

You can sort any column or a combination of columns through the Grid API using the Grid sort method:

import { SortingDirection } from 'igniteui-angular';

// Perform a case insensitive ascending sort on the ProductName column.
this.grid.sort({ fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true });

// Perform sorting on both the ProductName and Price columns.
this.grid.sort([
    { fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
    { fieldName: 'Price', dir: SortingDirection.Desc }
]);
Note

Sorting is performed using our DefaultSortingStrategy algorithm. Any IgxColumnComponent or ISortingExpression can use a custom implementation of the ISortingStrategy as a substitute algorithm. This is useful when custom sorting needs to be defined for complex template columns, or image columns, for example.

As with the filtering behavior, you can clear the sorting state by using the clearSort method:

// Removes the sorting state from the ProductName column
this.grid.clearSort('ProductName');

// Removes the sorting state from every column in the Grid
this.grid.clearSort();
Note

The sorting operation DOES NOT change the underlying data source of the Grid.

Initial sorting state

It is possible to set the initial sorting state of the Grid by passing an array of sorting expressions to the sortingExpressions property of the Grid.

public ngOnInit() {
    this.grid.sortingExpressions = [
        { fieldName: 'ProductName', dir: SortingDirection.Asc, ignoreCase: true },
        { fieldName: 'Price', dir: SortingDirection.Desc }
    ];
}
Note

If values of type string are used by a column of dataType Date, the Grid won't parse them to Date objects and using Grid sorting won't work as expected. If you want to use string objects, additional logic should be implemented on an application level, in order to parse the values to Date objects.

Remote Sorting

You can provide Grid's remote sorting by subscribing to onDataPreLoad and onSortingDone outputs. More information on how to use it you can find in the Grid Virtualization and Performance documentation.

Styling

To get started with styling the sorting behavior, we need to import the index file, where all the theme functions and component mixins live:

@import '~igniteui-angular/lib/core/styles/themes/index';

Following the simplest approach, we create a new theme that extends the igx-grid-theme and accepts the $sorted-header-icon-color parameter.

$custom-theme: igx-grid-theme(
    $sorted-header-icon-color: #51f942
);

The last step is to include the component mixins:

 @include igx-grid($custom-theme);
Note

If the component is using an Emulated ViewEncapsulation, it is necessary to penetrate this encapsulation using ::ng-deep:

:host {
 ::ng-deep {
   @include igx-grid($custom-theme);
 }
}

Defining a color palette

Instead of hardcoding the color values like we just did, we can achieve greater flexibility in terms of colors by using the igx-palette and igx-color functions.

igx-palette generates a color palette based on the primary and secondary colors that are passed:

$black-color: #292826;
$green-color: #51f942;

$custom-palette: igx-palette($primary: $black-color, $secondary: $green-color);

And then with igx-color we can easily retrieve color from the palette.

$custom-theme: igx-grid-theme(
    $sorted-header-icon-color: igx-color($custom-palette, "secondary", 700)
);
Note

The igx-color and igx-palette are powerful functions for generating and retrieving colors. Please refer to Palettes topic for detailed guidance on how to use them.

Using Schemas

Going further with the theming engine, you can build a robust and flexible structure that benefits from schemas. A schema is a recipe of a theme.

Extend one of the two predefined schemas, that are provided for every component, in this case - _light-grid:

// Extending the light grid schema
$custom-grid-schema: extend($_light-grid,
    (
        sorted-header-icon-color: (igx-color:('secondary', 700)),
        sortable-header-icon-hover-color: (igx-color:('primary', 300))
    )
);

In order to apply our custom schema we have to extend one of the globals (light or dark), which is basically pointing out the components with a custom schema, and after that add it to the respective component themes:

// Extending the global light-schema
$my-custom-schema: extend($light-schema, 
    (
        igx-grid: $custom-grid-schema
    )
);

// Defining our custom theme with the custom schema
$custom-theme: igx-grid-theme(
  $palette: $custom-palette,
  $schema: $my-custom-schema
);

Don't forget to include the themes in the same way as it was demonstrated above.

Demo


API References

Additional Resources

Our community is active and always welcoming to new ideas.