JavaScript Data Grid: Row Sorting

This page describes how to sort row data in the grid and how you can customise that sorting to match your requirements.

Enable Sorting

Enable sorting for columns by setting the sortable column definition attribute. You can then sort a column by clicking on the column header.

const gridOptions = {
    // enable sorting on 'name' and 'age' columns only
    columnDefs: [
        { field: 'name', sortable: true },
        { field: 'age', sortable: true },
        { field: 'address' },

    // other grid options ...

To enable sorting for all columns, set sorting in the default column definition.

const gridOptions = {
    // enable sorting on all columns by default
    defaultColDef: {
        sortable: true
    columnDefs: [
        { field: 'name' },
        { field: 'age' },
        // suppress sorting on address column
        { field: 'address', sortable: false },

    // other grid options ...

Custom Sorting

Custom sorting is provided at a column level by configuring a comparator on the column definition.

Override the default sorting order by providing a custom sort comparator.
  • valueA, valueB are the values to compare.
  • nodeA, nodeB are the corresponding RowNodes. Useful if additional details are required by the sort.
  • isDescending - true if sort direction is desc. Not to be used for inverting the return value as the grid already applies asc or desc ordering.
  • Return:
  • 0 valueA is the same as valueB
  • > 0 Sort valueA after valueB
  • < 0 Sort valueA before valueB
  • comparator = (
        valueA: any,
        valueB: any,
        nodeA: IRowNode<TData>,
        nodeB: IRowNode<TData>,
        isDescending: boolean
    ) => number;
    const gridOptions = {
        // enable sorting on all columns by default
        defaultColDef: {
            sortable: true
        columnDefs: [
                field: 'age',
                // simple number comparator
                comparator: (valueA, valueB, nodeA, nodeB, isDescending) => valueA - valueB
                field: 'name',
                // simple string comparator
                comparator: (valueA, valueB, nodeA, nodeB, isDescending) => {
                    if (valueA == valueB) return 0;
                    return (valueA > valueB) ? 1 : -1;
        // other grid options ...

    Custom Sorting Example

    Example below shows the following:

    • Default sorting on the Athlete column.
    • When the Year column is not sorted, it shows a custom icon (up/down arrow).
    • The Date column has strings as the row data, but has a custom comparator so that when you sort this column it sorts as dates, not as strings.

    Custom Sorting Groups Example

    When Row Grouping it is possible to override the sort order of the Row Group columns. If using the Auto Group Column, provide a comparator via the autoGroupColumnDef grid property.

    const gridOptions = {
        autoGroupColumnDef: {
            field: 'athlete',
            comparator: function(valueA, valueB, nodeA, nodeB, isDescending) {
                return (valueA == valueB) ? 0 : (valueA > valueB) ? 1 : -1;
        // other grid options ...

    Multi Column Sorting

    It is possible to sort by multiple columns. The default action for multiple column sorting is for the user to hold down Shift while clicking the column header. To change the default action to use the Ctrl key (or Command key on Apple) instead set the property multiSortKey='ctrl'.

    The example below demonstrates the following:

    • The grid sorts by Country then Athlete by default.
    • The property multiSortKey='ctrl' is set so multiple column sorting is achieved by holding down Ctrl (or Command on Apple) and selecting multiple columns.

    You can suppress the multi sorting behaviour by enabling the suppressMultiSort option, or force the behaviour without key press by enabling the alwaysMultiSort option.

    Sorting Animation

    To enable animation of the rows after sorting, set grid property animateRows=true.

    Sorting Order

    By default, the sorting order is as follows:

    ascending -> descending -> none.

    In other words, when you click a column that is not sorted, it will sort ascending. The next click will make it sort descending. Another click will remove the sort.

    It is possible to override this behaviour by providing your own sortingOrder on either the gridOptions or the colDef. If defined both in colDef and gridOptions, the colDef will get preference, allowing you to define a common default, and then tailor per column.

    Example: Sorting Order and Animation

    The example below shows animation of the rows plus different combinations of sorting orders as follows:

    • Default Columns: descending -> ascending -> no sort
    • Column Athlete: ascending -> descending
    • Column Age: descending -> ascending
    • Column Country: descending -> no sort
    • Column Year: ascending only

    Sorting API

    What sorting is applied is controlled via Column State. The below examples uses the Column State API to control column sorting.

    Accented Sort

    By default sorting doesn't take into consideration locale-specific characters. If you need to make your sort locale-specific you can configure this by setting the grid option accentedSort = true.

    Using this feature is more expensive; if you need to sort a very large amount of data, you might find that this causes the sort to be noticeably slower.

    The following example is configured to use this feature.


    It is also possible to perform some post-sorting if you require additional control over the sorted rows.

    This is provided via the postSortRows grid callback function as shown below:

    Callback to perform additional sorting after the grid has sorted the rows.
    postSortRows = (
        params: PostSortRowsParams<TData>
    ) => void;
    interface PostSortRowsParams<TData = any, TContext = any> {
      nodes: IRowNode<TData>[];
      // The grid api. 
      api: GridApi<TData>;
      // The column api. 
      columnApi: ColumnApi;
      // Application context as set on `gridOptions.context`. 
      context: TContext;
    const gridOptions = {
        postSortRows: params => {
            let rowNodes = params.rowNodes;
            // here we put Ireland rows on top while preserving the sort order
            let nextInsertPos = 0;
            for (let i = 0; i < rowNodes.length; i++) {
                const country = rowNodes[i];
                if (country === 'Ireland') {
                    rowNodes.splice(nextInsertPos, 0, rowNodes.splice(i, 1)[0]);
        // other grid options ...

    The following example uses this configuration to perform a post-sort on the rows. The custom function puts rows with Ireland at the top always.

    // Loading...