sorter.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. Copyright (c) 2004-2012, 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
  5. */
  6. if(!dojo._hasResource["dojo.data.util.sorter"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojo.data.util.sorter"] = true;
  8. dojo.provide("dojo.data.util.sorter");
  9. dojo.getObject("data.util.sorter", true, dojo);
  10. dojo.data.util.sorter.basicComparator = function( /*anything*/ a,
  11. /*anything*/ b){
  12. // summary:
  13. // Basic comparision function that compares if an item is greater or less than another item
  14. // description:
  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.
  18. //null is a problematic compare, so if null, we set to undefined.
  19. //Makes the check logic simple, compact, and consistent
  20. //And (null == undefined) === true, so the check later against null
  21. //works for undefined and is less bytes.
  22. var r = -1;
  23. if(a === null){
  24. a = undefined;
  25. }
  26. if(b === null){
  27. b = undefined;
  28. }
  29. if(a == b){
  30. r = 0;
  31. }else if(a > b || a == null){
  32. r = 1;
  33. }
  34. return r; //int {-1,0,1}
  35. };
  36. dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
  37. /*dojo.data.core.Read*/ store){
  38. // summary:
  39. // Helper function to generate the sorting function based off the list of sort attributes.
  40. // description:
  41. // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
  42. // it will look in the mapping for comparisons function for the attributes. If one is found, it will
  43. // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
  44. // Returns the sorting function for this particular list of attributes and sorting directions.
  45. //
  46. // sortSpec: array
  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:
  49. // {
  50. // attribute: "attributeName-string" || attribute,
  51. // descending: true|false; // Default is false.
  52. // }
  53. // store: object
  54. // The datastore object to look up item values from.
  55. //
  56. var sortFunctions=[];
  57. function createSortFunction(attr, dir, comp, s){
  58. //Passing in comp and s (comparator and store), makes this
  59. //function much faster.
  60. return function(itemA, itemB){
  61. var a = s.getValue(itemA, attr);
  62. var b = s.getValue(itemB, attr);
  63. return dir * comp(a,b); //int
  64. };
  65. }
  66. var sortAttribute;
  67. var map = store.comparatorMap;
  68. var bc = dojo.data.util.sorter.basicComparator;
  69. for(var i = 0; i < sortSpec.length; i++){
  70. sortAttribute = sortSpec[i];
  71. var attr = sortAttribute.attribute;
  72. if(attr){
  73. var dir = (sortAttribute.descending) ? -1 : 1;
  74. var comp = bc;
  75. if(map){
  76. if(typeof attr !== "string" && ("toString" in attr)){
  77. attr = attr.toString();
  78. }
  79. comp = map[attr] || bc;
  80. }
  81. sortFunctions.push(createSortFunction(attr,
  82. dir, comp, store));
  83. }
  84. }
  85. return function(rowA, rowB){
  86. var i=0;
  87. while(i < sortFunctions.length){
  88. var ret = sortFunctions[i++](rowA, rowB);
  89. if(ret !== 0){
  90. return ret;//int
  91. }
  92. }
  93. return 0; //int
  94. }; // Function
  95. };
  96. }