sorts datastore
by | string|object | the name(s) of property(-ies) by which data will be sorted |
dir | string | the sorting direction, "asc" by default |
as | string | the type of data, "string" by default |
data.sort("price", "asc");
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 strings
data.sort("#name#","asc","string");
//sorts by the price value; descending; sorts 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){
//a, b - data objects
return a.Version > b.Version ? 1 : -1;
}
data.sort(my_sorting, "desc"); //3rd parameter will be ignored
You can use a object with sorting settings as the first parameter of the sort method:
data.sort({
as:"string",
dir:"desc",
by:"package"
});
The second parameter of the sort method specifies the sorting direction and can have one of the two values:
The third parameter of the sort method is a string which represents the desired sorting mode: