2010-11-15 07:39:52 +00:00
|
|
|
/*
|
2011-11-08 16:40:44 +00:00
|
|
|
Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved.
|
2010-11-15 07:39:52 +00:00
|
|
|
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
|
|
|
see: http://dojotoolkit.org/license for details
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-03-04 16:02:28 +00:00
|
|
|
if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
|
dojo._hasResource["dojo.data.util.sorter"] = true;
|
2010-11-15 07:39:52 +00:00
|
|
|
dojo.provide("dojo.data.util.sorter");
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2011-11-08 16:40:44 +00:00
|
|
|
dojo.getObject("data.util.sorter", true, dojo);
|
|
|
|
|
|
|
|
dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
|
2011-03-04 16:02:28 +00:00
|
|
|
/*anything*/ b){
|
2011-11-08 16:40:44 +00:00
|
|
|
// summary:
|
2011-03-04 16:02:28 +00:00
|
|
|
// Basic comparision function that compares if an item is greater or less than another item
|
2011-11-08 16:40:44 +00:00
|
|
|
// description:
|
2011-03-04 16:02:28 +00:00
|
|
|
// returns 1 if a > b, -1 if a < b, 0 if equal.
|
|
|
|
// 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
|
|
|
|
// And compared to each other, null is equivalent to undefined.
|
|
|
|
|
|
|
|
//null is a problematic compare, so if null, we set to undefined.
|
|
|
|
//Makes the check logic simple, compact, and consistent
|
|
|
|
//And (null == undefined) === true, so the check later against null
|
|
|
|
//works for undefined and is less bytes.
|
|
|
|
var r = -1;
|
|
|
|
if(a === null){
|
|
|
|
a = undefined;
|
|
|
|
}
|
|
|
|
if(b === null){
|
|
|
|
b = undefined;
|
|
|
|
}
|
|
|
|
if(a == b){
|
2011-11-08 16:40:44 +00:00
|
|
|
r = 0;
|
2011-03-04 16:02:28 +00:00
|
|
|
}else if(a > b || a == null){
|
2011-11-08 16:40:44 +00:00
|
|
|
r = 1;
|
2011-03-04 16:02:28 +00:00
|
|
|
}
|
|
|
|
return r; //int {-1,0,1}
|
2010-11-15 07:39:52 +00:00
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
|
|
|
dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
|
|
|
|
/*dojo.data.core.Read*/ store){
|
2011-11-08 16:40:44 +00:00
|
|
|
// summary:
|
2011-03-04 16:02:28 +00:00
|
|
|
// Helper function to generate the sorting function based off the list of sort attributes.
|
2011-11-08 16:40:44 +00:00
|
|
|
// description:
|
2011-03-04 16:02:28 +00:00
|
|
|
// The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
|
|
|
|
// it will look in the mapping for comparisons function for the attributes. If one is found, it will
|
|
|
|
// use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
|
|
|
|
// Returns the sorting function for this particular list of attributes and sorting directions.
|
|
|
|
//
|
|
|
|
// sortSpec: array
|
|
|
|
// A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
|
|
|
|
// The objects should be formatted as follows:
|
|
|
|
// {
|
|
|
|
// attribute: "attributeName-string" || attribute,
|
|
|
|
// descending: true|false; // Default is false.
|
|
|
|
// }
|
|
|
|
// store: object
|
|
|
|
// The datastore object to look up item values from.
|
|
|
|
//
|
|
|
|
var sortFunctions=[];
|
|
|
|
|
|
|
|
function createSortFunction(attr, dir, comp, s){
|
|
|
|
//Passing in comp and s (comparator and store), makes this
|
|
|
|
//function much faster.
|
|
|
|
return function(itemA, itemB){
|
|
|
|
var a = s.getValue(itemA, attr);
|
|
|
|
var b = s.getValue(itemB, attr);
|
|
|
|
return dir * comp(a,b); //int
|
|
|
|
};
|
|
|
|
}
|
|
|
|
var sortAttribute;
|
|
|
|
var map = store.comparatorMap;
|
|
|
|
var bc = dojo.data.util.sorter.basicComparator;
|
|
|
|
for(var i = 0; i < sortSpec.length; i++){
|
|
|
|
sortAttribute = sortSpec[i];
|
|
|
|
var attr = sortAttribute.attribute;
|
|
|
|
if(attr){
|
|
|
|
var dir = (sortAttribute.descending) ? -1 : 1;
|
|
|
|
var comp = bc;
|
|
|
|
if(map){
|
|
|
|
if(typeof attr !== "string" && ("toString" in attr)){
|
|
|
|
attr = attr.toString();
|
|
|
|
}
|
|
|
|
comp = map[attr] || bc;
|
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
sortFunctions.push(createSortFunction(attr,
|
2011-03-04 16:02:28 +00:00
|
|
|
dir, comp, store));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return function(rowA, rowB){
|
|
|
|
var i=0;
|
|
|
|
while(i < sortFunctions.length){
|
|
|
|
var ret = sortFunctions[i++](rowA, rowB);
|
|
|
|
if(ret !== 0){
|
|
|
|
return ret;//int
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 16:40:44 +00:00
|
|
|
return 0; //int
|
2011-03-04 16:02:28 +00:00
|
|
|
}; // Function
|
2010-11-15 07:39:52 +00:00
|
|
|
};
|
2011-03-04 16:02:28 +00:00
|
|
|
|
2010-11-15 07:39:52 +00:00
|
|
|
}
|