Documentation

sort

sorts rows in DataTable

void sort(string by, [string dir,string as] );

Parameters

bystringthe name(s) of property(s) by which data will be sorted
dirstringthe sorting direction, default is "asc"
asstringthe type of data , default is "string"

See also

Example

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

Details

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 name value; ascedent; sorted values as strings
data.sort("#name#","asc","string");
//sorts by price value; descedant; sorted values as numbers
data.sort("#price#","desc","int");

The property name should be included into sharps.

Instead of default sorting types, you can use your own method ( can be used for complex data types ):

function sort_by_length(data1, data2){
    return data1.length>data2.length ? 1:-1;
}
 
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, "des"); //3rd parameter will be ignored

Alternative syntax

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

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

Sorting directions

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

  • "asc" - for ascending sorting
  • "desc" - for decrescent sorting

Sorting types

The third parameter of the sort method may be string which represents the data type:

  • "int" - sorts as interger values
  • "string" - sorts as string
  • "string_strict" - sorts as string, case-sensitive
Back to top