]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/util/sorter.js.uncompressed.js
1 define("dojo/data/util/sorter", ["../../_base/lang"], function(lang
){
3 // dojo/data/util/sorter
8 lang
.setObject("dojo.data.util.sorter", sorter
);
10 sorter
.basicComparator = function( /*anything*/ a
,
13 // Basic comparison function that compares if an item is greater or less than another item
15 // returns 1 if a > b, -1 if a < b, 0 if equal.
16 // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
17 // And compared to each other, null is equivalent to undefined.
19 //null is a problematic compare, so if null, we set to undefined.
20 //Makes the check logic simple, compact, and consistent
21 //And (null == undefined) === true, so the check later against null
22 //works for undefined and is less bytes.
32 }else if(a
> b
|| a
== null){
35 return r
; //int {-1,0,1}
38 sorter
.createSortFunction = function( /* attributes[] */sortSpec
, /*dojo/data/api/Read*/ store){
40 // Helper function to generate the sorting function based off the list of sort attributes.
42 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
43 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
44 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
45 // 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);