sort

Specifies the sort expression for the sorting data transformation. The sort setting can be specified in one of the following formats:

1. As an object literal, specifying a single field definition to sort by:

var dataSource = new shield.DataSource({
    sort: { path: "name", desc: true }
});

2. As an array of object literals specifying multiple field definitions to sort by. In this case, sorting is applied sequentially from the first to the last field definition:

var dataSource = new shield.DataSource({
    sort: [
        { path: "id" },
        { path: "name", desc: true }
    ]
});

3. As a function expression specifying the sorting function to use:

var dataSource = new shield.DataSource({
    sort: function (a, b) {
        return a.price - b.price;
    }
});

This function is provided with two arguments - the left-hand side data item (a) and the right-hand side data item (b) in the data array. The function must return a numeric value as follows:

Return Value Semantics
positive value a > b
negative value a < b
0 a == b

The sort function is semantically equivalent to Array.prototype.sort(). In the above code snippet, a positive return value would indicate item a comes before item b when compared by price. If the operands switch placed, the exact reverse ordering would be applied:

var dataSource = new shield.DataSource({
    sort: function (a, b) {
        return b.price - a.price;
    }
});

This approach can be used to implement the equivalent of descending sorting.