1 if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojo.data.util.sorter"] = true;
3 dojo.provide("dojo.data.util.sorter");
5 dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
8 // Basic comparision function that compares if an item is greater or less than another item
10 // returns 1 if a > b, -1 if a < b, 0 if equal.
11 // undefined values are treated as larger values so that they're pushed to the end of the list.
14 if(a > b || typeof a === "undefined" || a === null){
16 }else if(a < b || typeof b === "undefined" || b === null){
19 return ret; //int, {-1,0,1}
22 dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
23 /*dojo.data.core.Read*/ store){
25 // Helper function to generate the sorting function based off the list of sort attributes.
27 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
28 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
29 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
30 // Returns the sorting function for this particular list of attributes and sorting directions.
33 // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
34 // The objects should be formatted as follows:
36 // attribute: "attributeName-string" || attribute,
37 // descending: true|false; // Default is false.
40 // The datastore object to look up item values from.
44 function createSortFunction(attr, dir){
45 return function(itemA, itemB){
46 var a = store.getValue(itemA, attr);
47 var b = store.getValue(itemB, attr);
48 //See if we have a override for an attribute comparison.
49 var comparator = null;
50 if(store.comparatorMap){
51 if(typeof attr !== "string"){
52 attr = store.getIdentity(attr);
54 comparator = store.comparatorMap[attr]||dojo.data.util.sorter.basicComparator;
56 comparator = comparator||dojo.data.util.sorter.basicComparator;
57 return dir * comparator(a,b); //int
61 for(var i = 0; i < sortSpec.length; i++){
62 sortAttribute = sortSpec[i];
63 if(sortAttribute.attribute){
64 var direction = (sortAttribute.descending) ? -1 : 1;
65 sortFunctions.push(createSortFunction(sortAttribute.attribute, direction));
69 return function(rowA, rowB){
71 while(i < sortFunctions.length){
72 var ret = sortFunctions[i++](rowA, rowB);