arraysort-debug.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. YUI 3.4.1 (build 4118)
  3. Copyright 2011 Yahoo! Inc. All rights reserved.
  4. Licensed under the BSD License.
  5. http://yuilibrary.com/license/
  6. */
  7. YUI.add('arraysort', function(Y) {
  8. /**
  9. * Provides a case-insenstive comparator which can be used for array sorting.
  10. *
  11. * @module arraysort
  12. */
  13. var LANG = Y.Lang,
  14. ISVALUE = LANG.isValue,
  15. ISSTRING = LANG.isString;
  16. /**
  17. * Provides a case-insenstive comparator which can be used for array sorrting.
  18. *
  19. * @class ArraySort
  20. */
  21. Y.ArraySort = {
  22. /**
  23. * Comparator function for simple case-insensitive string sorting.
  24. *
  25. * @method compare
  26. * @param a {Object} First sort argument.
  27. * @param b {Object} Second sort argument.
  28. * @param desc {Boolean} True if sort direction is descending, false if
  29. * sort direction is ascending.
  30. * @return {Boolean} Return -1 when a < b. Return 0 when a = b.
  31. * Return 1 when a > b.
  32. */
  33. compare: function(a, b, desc) {
  34. if(!ISVALUE(a)) {
  35. if(!ISVALUE(b)) {
  36. return 0;
  37. }
  38. else {
  39. return 1;
  40. }
  41. }
  42. else if(!ISVALUE(b)) {
  43. return -1;
  44. }
  45. if(ISSTRING(a)) {
  46. a = a.toLowerCase();
  47. }
  48. if(ISSTRING(b)) {
  49. b = b.toLowerCase();
  50. }
  51. if(a < b) {
  52. return (desc) ? 1 : -1;
  53. }
  54. else if (a > b) {
  55. return (desc) ? -1 : 1;
  56. }
  57. else {
  58. return 0;
  59. }
  60. }
  61. };
  62. }, '3.4.1' ,{requires:['yui-base']});