1 define("dojo/data/util/sorter", ["dojo/_base/lang"], function(lang) {
3 // dojo/data/util/sorter
7 var sorter = lang.getObject("dojo.data.util.sorter", true);
9 sorter.basicComparator = function( /*anything*/ a,
12 // Basic comparision function that compares if an item is greater or less than another item
14 // returns 1 if a > b, -1 if a < b, 0 if equal.
15 // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
16 // And compared to each other, null is equivalent to undefined.
18 //null is a problematic compare, so if null, we set to undefined.
19 //Makes the check logic simple, compact, and consistent
20 //And (null == undefined) === true, so the check later against null
21 //works for undefined and is less bytes.
31 }else if(a > b || a == null){
34 return r; //int {-1,0,1}
37 sorter.createSortFunction = function( /* attributes array */sortSpec, /*dojo.data.core.Read*/ store){
39 // Helper function to generate the sorting function based off the list of sort attributes.
41 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
42 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
43 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
44 // Returns the sorting function for this particular list of attributes and sorting directions.
47 // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
48 // The objects should be formatted as follows:
50 // attribute: "attributeName-string" || attribute,
51 // descending: true|false; // Default is false.
54 // The datastore object to look up item values from.
58 function createSortFunction(attr, dir, comp, s){
59 //Passing in comp and s (comparator and store), makes this
60 //function much faster.
61 return function(itemA, itemB){
62 var a = s.getValue(itemA, attr);
63 var b = s.getValue(itemB, attr);
64 return dir * comp(a,b); //int
68 var map = store.comparatorMap;
69 var bc = sorter.basicComparator;
70 for(var i = 0; i < sortSpec.length; i++){
71 sortAttribute = sortSpec[i];
72 var attr = sortAttribute.attribute;
74 var dir = (sortAttribute.descending) ? -1 : 1;
77 if(typeof attr !== "string" && ("toString" in attr)){
78 attr = attr.toString();
80 comp = map[attr] || bc;
82 sortFunctions.push(createSortFunction(attr,
86 return function(rowA, rowB){
88 while(i < sortFunctions.length){
89 var ret = sortFunctions[i++](rowA, rowB);