DHTMLX Touch documentation

sort(by, dir, as)

  • Type: Method
  • File: core/datastore.js
  • Implemented in: DataStore
  • Included in:

sorts datastore

Parameters

  • by - template for sorted value
    • template
    • function
    • hash of parameters
  • dir - sorting direction (optional)
  • as - type of data sorting (optional)
    • function
    • string, one from the next list: string,string_strict,int

Returns

Details

Usage examples

Method sort can be used with different count of parameters.

First parameter

The most common use case of sort method is using first parameter as name of the property:

data.sort("#Package#","asc","string");

Property name should be included into sharps.

First parameter of sort method may be function, which recives 2 DataStore objects. In such case you can access to all properties of comparing objects:

data.sort(by_func,"des"); //3rd parameter will be ignored
function by_func(a,b){
    //a, b - DataStore objects
    return a.Version > b.Version ? 1 : -1;
}

Possible return values of this fucntion are 0,1,-1. '1' means first object is greater than second object, '-1' means first object is less than second object, '0' means 2 objects are equal

Also you can use hash of sorting settings as first parameter of sort method:

data.sort({
                as:"string",
                dir:"des",
                by:"#Package#"
            }); //2nd and 3rd parameters will be ignored
Second parameter

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

  • “asc” for ascending sorting
  • “desc” for decrescent sorting.
Third parameter

Third parameter of sort method may be string which represeints type of data:

  • “int” - sort as interger values
  • “string” - sort as string
  • “string_strict” - sort as string, letters case will be considered
data.sort("#Version#","des","int");

Also 3rd parameter may be function, which recives 2 parameters - content of corresponding property. In such case you will not have access to all properties of comparing objects:

data.sort("#Maintainer#","des",as_func);
function as_func(Maintainer1,Maintainer2){
    //Maintainer1,Maintainer2 - content of Maintainer property of 2 object from DataStore
    return Maintainer1.length>Maintainer2.length ? 1:-1;
}

As for first parameter, this function should return 1,-1 or 0.