sort
sorts datastore
function sort( // the name(s) of property(-ies) by which data will be sorted by: string|WebixSortConfig, // the sorting direction, "asc" by default dir?: string, // the type of data, "string" by default as?: string): void
Example
Section titled “Example”data.sort("price", "asc");
also check the next samples:
Details
Section titled “Details”Usage examples
Section titled “Usage examples”The method sort can be used with different count of parameters.
The most common use case of the sort method is using the first parameter as the name of a property:
//sorts by the name value; ascending; sorts values as stringsdata.sort("#name#","asc","string");//sorts by the price value; descending; sorts values as numbersdata.sort("#price#","desc","int");
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:
- a (object) - the first data item
- b (object) - the second data item
- prop(string) - the name of the field that you want to sort by.
data.sort("#name#", "asc", sort_by_length);
Possible return values of this function are 0, 1, -1.
- 1 means that the first object is greater than the second object;
- -1 means that the first object is less than the second object;
- 0 means that 2 objects are equal.
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){ //a, b - data objects return a.Version > b.Version ? 1 : -1;}data.sort(my_sorting, "desc"); //3rd parameter will be ignored
Alternative syntax
Section titled “Alternative syntax”You can use an object with sorting settings as the first parameter of the sort method:
data.sort({ as:"string", dir:"desc", by:"package"});
Sorting directions
Section titled “Sorting directions”The second parameter of the sort method specifies the sorting direction and can have one of the two values:
- “asc” - for ascending sorting
- “desc” - for descending sorting
Sorting types
Section titled “Sorting types”The third parameter of the sort method is a string which represents the desired sorting mode:
- “int” - compares numeric values;
- “date” - compares dates;
- “string” - compares string values;
- “string_strict” - case-sensitive “string”;
- “text” - compares visible item text (including template), datagrid only;
- “string_locale” - compares string values based on the locale;
- “string_locale_strict” - case-sensitive “string_locale”;
- “text_locale” - compares visible item text based on the locale (including template), datagrid only;
- “server” - issues a server side request for a sorted dataset;
- “raw” - basic sorter with simple comparison (a>b and vice versa);
- or, you can set a custom sorting type.
See also
Section titled “See also”Articles