sorter.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import Converter from 'yarn-ui/utils/converter';
  2. import Ember from 'ember';
  3. export default {
  4. _initElapsedTimeSorter: function() {
  5. Ember.$.extend(Ember.$.fn.dataTableExt.oSort, {
  6. "elapsed-time-pre": function (a) {
  7. return Converter.padding(Converter.elapsedTimeToMs(a), 20);
  8. },
  9. });
  10. },
  11. _initNaturalSorter: function() {
  12. Ember.$.extend(Ember.$.fn.dataTableExt.oSort, {
  13. "natural-asc": function (a, b) {
  14. return naturalSort(a,b);
  15. },
  16. "natural-desc": function (a, b) {
  17. return naturalSort(a,b) * -1;
  18. },
  19. });
  20. },
  21. initDataTableSorter: function() {
  22. this._initElapsedTimeSorter();
  23. this._initNaturalSorter();
  24. },
  25. };
  26. /**
  27. * Natural sort implementation.
  28. * Typically used to sort application Ids'.
  29. */
  30. function naturalSort(a, b) {
  31. var diff = a.length - b.length;
  32. if (diff != 0) {
  33. var splitA = a.split("_");
  34. var splitB = b.split("_");
  35. if (splitA.length != splitB.length) {
  36. return a.localeCompare(b);
  37. }
  38. for (var i = 1; i < splitA.length; i++) {
  39. var splitdiff = splitA[i].length - splitB[i].length;
  40. if (splitdiff != 0) {
  41. return splitdiff;
  42. }
  43. var splitCompare = splitA[i].localeCompare(splitB[i]);
  44. if (splitCompare != 0) {
  45. return splitCompare;
  46. }
  47. }
  48. return diff;
  49. }
  50. return a.localeCompare(b);
  51. }