How to implement sorting?

Common information

While using the components that operate on data, such as grid, list or chart, you often need to implement sorting.

The parameter, which answers for built-in component sorting, is 'sort'.

It has the following attributes:

  • by - (template) a template which defines the data that will be sorted
  • dir - (asc or dsc)a sorting direction
  • as - (int, string or string_strict (case-sensitive 'string')) a build-in or custom sorting method
sort{  by:"#sales#",
       dir:"asc",
       as:"int"
}

Custom sorting method

There are three defined sorting methods: int, string and string_strict. If neither of these methods is appropriate, you may set a new one. A sorting method is defined by a function. This function is called for each pair of values and returns 1,-1 or 0:

  • 1 - the object with the first value in pair must go before the second one
  • -1 - the second object goes before the first
  • 0 - the order of both objects doesn't change

For example, if we need to sort by time property that consists of minutes and seconds divided by ”:” than the following custom sorting can be used:

dhx.ui({
...
             sort({
                    by:"#time#",
                    dir:"asc",
                    as: "sortTime"
                 }),
             ...
})
function sortTime(a,b){
        a = a.split(":");
        b = b.split(":");
        if(a[0] > b[0]) return 1;
        else if(a[0] < b[0]) return -1;
        else{
            if(a[1] > b[1]) return 1;
            else if(a[1] < b[1]) return -1;
 	    return 0;
	}
}