yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
define(["../../_base/lang"], function(lang){
    // module:
    //      dojo/data/util/sorter
    // summary:
    //      TODOC
 
var sorter = {};
lang.setObject("dojo.data.util.sorter", sorter);
 
sorter.basicComparator = function(  /*anything*/ a,
                                                    /*anything*/ b){
    // summary:
    //      Basic comparison function that compares if an item is greater or less than another item
    // description:
    //      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){
        r = 0;
    }else if(a > b || a == null){
        r = 1;
    }
    return r; //int {-1,0,1}
};
 
sorter.createSortFunction = function(   /* attributes[] */sortSpec, /*dojo/data/api/Read*/ store){
    // summary:
    //      Helper function to generate the sorting function based off the list of sort attributes.
    // description:
    //      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:
    //      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:
    //      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 = 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;
            }
            sortFunctions.push(createSortFunction(attr,
                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
            }
        }
        return 0; //int
    }; // Function
};
 
return sorter;
});