sorter.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. define("dojo/data/util/sorter", ["../../_base/lang"], function(lang) {
  2. // module:
  3. // dojo/data/util/sorter
  4. // summary:
  5. // TODOC
  6. var sorter = lang.getObject("dojo.data.util.sorter", true);
  7. sorter.basicComparator = function( /*anything*/ a,
  8. /*anything*/ b){
  9. // summary:
  10. // Basic comparision function that compares if an item is greater or less than another item
  11. // description:
  12. // returns 1 if a > b, -1 if a < b, 0 if equal.
  13. // 'null' values (null, undefined) are treated as larger values so that they're pushed to the end of the list.
  14. // And compared to each other, null is equivalent to undefined.
  15. //null is a problematic compare, so if null, we set to undefined.
  16. //Makes the check logic simple, compact, and consistent
  17. //And (null == undefined) === true, so the check later against null
  18. //works for undefined and is less bytes.
  19. var r = -1;
  20. if(a === null){
  21. a = undefined;
  22. }
  23. if(b === null){
  24. b = undefined;
  25. }
  26. if(a == b){
  27. r = 0;
  28. }else if(a > b || a == null){
  29. r = 1;
  30. }
  31. return r; //int {-1,0,1}
  32. };
  33. sorter.createSortFunction = function( /* attributes array */sortSpec, /*dojo.data.core.Read*/ store){
  34. // summary:
  35. // Helper function to generate the sorting function based off the list of sort attributes.
  36. // description:
  37. // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
  38. // it will look in the mapping for comparisons function for the attributes. If one is found, it will
  39. // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
  40. // Returns the sorting function for this particular list of attributes and sorting directions.
  41. //
  42. // sortSpec: array
  43. // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
  44. // The objects should be formatted as follows:
  45. // {
  46. // attribute: "attributeName-string" || attribute,
  47. // descending: true|false; // Default is false.
  48. // }
  49. // store: object
  50. // The datastore object to look up item values from.
  51. //
  52. var sortFunctions=[];
  53. function createSortFunction(attr, dir, comp, s){
  54. //Passing in comp and s (comparator and store), makes this
  55. //function much faster.
  56. return function(itemA, itemB){
  57. var a = s.getValue(itemA, attr);
  58. var b = s.getValue(itemB, attr);
  59. return dir * comp(a,b); //int
  60. };
  61. }
  62. var sortAttribute;
  63. var map = store.comparatorMap;
  64. var bc = sorter.basicComparator;
  65. for(var i = 0; i < sortSpec.length; i++){
  66. sortAttribute = sortSpec[i];
  67. var attr = sortAttribute.attribute;
  68. if(attr){
  69. var dir = (sortAttribute.descending) ? -1 : 1;
  70. var comp = bc;
  71. if(map){
  72. if(typeof attr !== "string" && ("toString" in attr)){
  73. attr = attr.toString();
  74. }
  75. comp = map[attr] || bc;
  76. }
  77. sortFunctions.push(createSortFunction(attr,
  78. dir, comp, store));
  79. }
  80. }
  81. return function(rowA, rowB){
  82. var i=0;
  83. while(i < sortFunctions.length){
  84. var ret = sortFunctions[i++](rowA, rowB);
  85. if(ret !== 0){
  86. return ret;//int
  87. }
  88. }
  89. return 0; //int
  90. }; // Function
  91. };
  92. return sorter;
  93. });