]>
git.wh0rd.org - tt-rss.git/blob - lib/dojo/data/util/sorter.js
2 Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
3 Available via Academic Free License >= 2.1 OR the modified BSD license.
4 see: http://dojotoolkit.org/license for details
8 if(!dojo
._hasResource
["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
9 dojo
._hasResource
["dojo.data.util.sorter"] = true;
10 dojo
.provide("dojo.data.util.sorter");
12 dojo
.data
.util
.sorter
.basicComparator = function( /*anything*/ a
,
15 // Basic comparision function that compares if an item is greater or less than another item
17 // returns 1 if a > b, -1 if a < b, 0 if equal.
18 // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
19 // And compared to each other, null is equivalent to undefined.
21 //null is a problematic compare, so if null, we set to undefined.
22 //Makes the check logic simple, compact, and consistent
23 //And (null == undefined) === true, so the check later against null
24 //works for undefined and is less bytes.
34 }else if(a
> b
|| a
== null){
37 return r
; //int {-1,0,1}
40 dojo
.data
.util
.sorter
.createSortFunction = function( /* attributes array */sortSpec
,
41 /*dojo.data.core.Read*/ store
){
43 // Helper function to generate the sorting function based off the list of sort attributes.
45 // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
46 // it will look in the mapping for comparisons function for the attributes. If one is found, it will
47 // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
48 // Returns the sorting function for this particular list of attributes and sorting directions.
51 // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
52 // The objects should be formatted as follows:
54 // attribute: "attributeName-string" || attribute,
55 // descending: true|false; // Default is false.
58 // The datastore object to look up item values from.
62 function createSortFunction(attr
, dir
, comp
, s
){
63 //Passing in comp and s (comparator and store), makes this
64 //function much faster.
65 return function(itemA
, itemB
){
66 var a
= s
.getValue(itemA
, attr
);
67 var b
= s
.getValue(itemB
, attr
);
68 return dir
* comp(a
,b
); //int
72 var map
= store
.comparatorMap
;
73 var bc
= dojo
.data
.util
.sorter
.basicComparator
;
74 for(var i
= 0; i
< sortSpec
.length
; i
++){
75 sortAttribute
= sortSpec
[i
];
76 var attr
= sortAttribute
.attribute
;
78 var dir
= (sortAttribute
.descending
) ? -1 : 1;
81 if(typeof attr
!== "string" && ("toString" in attr
)){
82 attr
= attr
.toString();
84 comp
= map
[attr
] || bc
;
86 sortFunctions
.push(createSortFunction(attr
,
90 return function(rowA
, rowB
){
92 while(i
< sortFunctions
.length
){
93 var ret
= sortFunctions
[i
++](rowA
, rowB
);