Edit this page

columns.sortable.compare Function

A JavaScript function which is used to compare the values. It has the same signature as the compare function accepted by Array.sort.

The basic function implementation is as follows (pseudo-code):

    function compare(a, b, descending) {
      if (a is less than b by some ordering criterion) {
        return -1;
      }

      if (a is greater than b by the ordering criterion) {
        return 1;
      }

      // a must be equal to b
      return 0;
    }

One notable exception is that we also supply a third parameter that indicates the sort direction (true for descending). See How-to: Stable Sort in Chrome for more details on how this can be useful.

Example - define custom compare function

<div id="grid"></div>
<script>
    var numbers = {
        "one"  : 1,
        "two"  : 2,
        "three": 3
    };

    var dataSource = new kendo.data.DataSource({
        data: [
            { id: 1, item: "two" },
            { id: 2, item: "one" },
            { id: 3, item: "three" }
        ]
    });

    $("#grid").kendoGrid({
        dataSource: dataSource,
        sortable: true,
        columns: [{
            field: "item",
            sortable: {
                compare: function(a, b) {
                    return numbers[a.item] - numbers[b.item];
                }
            }
        }]
    });

</script>