/public/javascripts/dojo/release/dojo/dojo/data/util/sorter.js

http://enginey.googlecode.com/ · JavaScript · 88 lines · 51 code · 8 blank · 29 comment · 12 complexity · dcd2e632656e06c9f041a2165ff7ba26 MD5 · raw file

  1. /*
  2. Copyright (c) 2004-2008, 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.data.util.sorter.basicComparator = function( /*anything*/ a,
  10. /*anything*/ b){
  11. // summary:
  12. // Basic comparision function that compares if an item is greater or less than another item
  13. // description:
  14. // returns 1 if a > b, -1 if a < b, 0 if equal.
  15. // undefined values are treated as larger values so that they're pushed to the end of the list.
  16. var ret = 0;
  17. if(a > b || typeof a === "undefined" || a === null){
  18. ret = 1;
  19. }else if(a < b || typeof b === "undefined" || b === null){
  20. ret = -1;
  21. }
  22. return ret; //int, {-1,0,1}
  23. };
  24. dojo.data.util.sorter.createSortFunction = function( /* attributes array */sortSpec,
  25. /*dojo.data.core.Read*/ store){
  26. // summary:
  27. // Helper function to generate the sorting function based off the list of sort attributes.
  28. // description:
  29. // The sort function creation will look for a property on the store called 'comparatorMap'. If it exists
  30. // it will look in the mapping for comparisons function for the attributes. If one is found, it will
  31. // use it instead of the basic comparator, which is typically used for strings, ints, booleans, and dates.
  32. // Returns the sorting function for this particular list of attributes and sorting directions.
  33. //
  34. // sortSpec: array
  35. // A JS object that array that defines out what attribute names to sort on and whether it should be descenting or asending.
  36. // The objects should be formatted as follows:
  37. // {
  38. // attribute: "attributeName-string" || attribute,
  39. // descending: true|false; // Default is false.
  40. // }
  41. // store: object
  42. // The datastore object to look up item values from.
  43. //
  44. var sortFunctions=[];
  45. function createSortFunction(attr, dir){
  46. return function(itemA, itemB){
  47. var a = store.getValue(itemA, attr);
  48. var b = store.getValue(itemB, attr);
  49. //See if we have a override for an attribute comparison.
  50. var comparator = null;
  51. if(store.comparatorMap){
  52. if(typeof attr !== "string"){
  53. attr = store.getIdentity(attr);
  54. }
  55. comparator = store.comparatorMap[attr]||dojo.data.util.sorter.basicComparator;
  56. }
  57. comparator = comparator||dojo.data.util.sorter.basicComparator;
  58. return dir * comparator(a,b); //int
  59. };
  60. }
  61. var sortAttribute;
  62. for(var i = 0; i < sortSpec.length; i++){
  63. sortAttribute = sortSpec[i];
  64. if(sortAttribute.attribute){
  65. var direction = (sortAttribute.descending) ? -1 : 1;
  66. sortFunctions.push(createSortFunction(sortAttribute.attribute, direction));
  67. }
  68. }
  69. return function(rowA, rowB){
  70. var i=0;
  71. while(i < sortFunctions.length){
  72. var ret = sortFunctions[i++](rowA, rowB);
  73. if(ret !== 0){
  74. return ret;//int
  75. }
  76. }
  77. return 0; //int
  78. }; // Function
  79. };
  80. }