sort

sorts rows in DataTable

void sort(string|object|array|WebixCallback by, [string dir,string as] );
by
string|object|array|WebixCallbackthe property name(s) to sort by, a sorting settings object, an array of sorting configs, or a custom sorting function dir
stringthe sorting direction, "asc" by default as
stringthe type of data, "string" by default

Example

data.sort("price", "asc");

Related samples

Details

Usage examples

The method sort can be used with different parameters.

Most commonly, you would use the sort method to sort by the column value in the descending or ascending direction. For example:

// by name value; ascending; sort values as strings
data.sort("#name#","asc","string");
// by price value; descending; sort values as numbers
data.sort("#price#","desc","int");

The property name should be included into hashes.

Instead of default sorting types, you can use your own function ( can be used for complex data types ). The parameters of this function are the following:

function sort_by_length(data1, data2, prop){
    return data1[prop].length > data2[prop].length ? 1 : -1;
}
 
data.sort("#name#", "asc", sort_by_length);

Possible return values of this function are 0,1,-1.

If you need to access multiple properties of an object during sorting or you need to have some complex sorting logic, use a function as the first parameter.

function my_sorting(a, b, id){
    //a, b - data objects, id - id of the column, from which the sorting was invoked
    return a.Version > b.Version ? 1 : -1;
}
data.sort(my_sorting, "desc"); //3rd parameter will be ignored

Alternative syntax

You can use a object with sorting settings as the first parameter of the sort method:

data.sort({
    as:"string",
    dir:"desc",
    by:"package"
});

Multi-column sorting

In the case of sorting by multiple columns programmatically, the sort method can receive an array with sorting configs:

data.sort([
  { by:"some", dir:"asc:" },
  { by:"other", dir:"desc" }
]);

Related sample:  DataTable: Sorting By Multiple Columns

Sorting directions

Possible values for the second parameter of the sort method is string which represents sorting direction:

Sorting types

The third parameter of the sort method is a string which represents the desired sorting mode:

See also
  • API
  • Articles
  • Back to top
    Join Our Forum
    We've retired comments here. Visit our forum for faster technical support, connect with other developers, and share your feedback there.
    If you have not checked yet, be sure to visit site of our main product Webix ui library and page of javascript data grid product.