natural.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Data can often be a complicated mix of numbers and letters (file names
  3. * are a common example) and sorting them in a natural manner is quite a
  4. * difficult problem.
  5. *
  6. * Fortunately a deal of work has already been done in this area by other
  7. * authors - the following plug-in uses the [naturalSort() function by Jim
  8. * Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables.
  9. *
  10. * @name Natural sorting
  11. * @summary Sort data with a mix of numbers and letters _naturally_.
  12. * @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support)
  13. * @author [Michael Buehler] (https://github.com/AnimusMachina)
  14. *
  15. * @example
  16. * $('#example').dataTable( {
  17. * columnDefs: [
  18. * { type: 'natural', targets: 0 }
  19. * ]
  20. * } );
  21. *
  22. * Html can be stripped from sorting by using 'natural-nohtml' such as
  23. *
  24. * $('#example').dataTable( {
  25. * columnDefs: [
  26. * { type: 'natural-nohtml', targets: 0 }
  27. * ]
  28. * } );
  29. *
  30. */
  31. (function() {
  32. /*
  33. * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
  34. * Author: Jim Palmer (based on chunking idea from Dave Koelle)
  35. * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
  36. * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
  37. */
  38. function naturalSort (a, b, html) {
  39. var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
  40. sre = /(^[ ]*|[ ]*$)/g,
  41. dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
  42. hre = /^0x[0-9a-f]+$/i,
  43. ore = /^0/,
  44. htmre = /(<([^>]+)>)/ig,
  45. // convert all to strings and trim()
  46. x = a.toString().replace(sre, '') || '',
  47. y = b.toString().replace(sre, '') || '';
  48. // remove html from strings if desired
  49. if (!html) {
  50. x = x.replace(htmre, '');
  51. y = y.replace(htmre, '');
  52. }
  53. // chunk/tokenize
  54. var xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
  55. yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
  56. // numeric, hex or date detection
  57. xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)),
  58. yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
  59. // first try and sort Hex codes or Dates
  60. if (yD) {
  61. if ( xD < yD ) {
  62. return -1;
  63. }
  64. else if ( xD > yD ) {
  65. return 1;
  66. }
  67. }
  68. // natural sorting through split numeric strings and default strings
  69. for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
  70. // find floats not starting with '0', string or 0 if not defined (Clint Priest)
  71. var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc], 10) || xN[cLoc] || 0;
  72. var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc], 10) || yN[cLoc] || 0;
  73. // handle numeric vs string comparison - number < string - (Kyle Adams)
  74. if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
  75. return (isNaN(oFxNcL)) ? 1 : -1;
  76. }
  77. // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
  78. else if (typeof oFxNcL !== typeof oFyNcL) {
  79. oFxNcL += '';
  80. oFyNcL += '';
  81. }
  82. if (oFxNcL < oFyNcL) {
  83. return -1;
  84. }
  85. if (oFxNcL > oFyNcL) {
  86. return 1;
  87. }
  88. }
  89. return 0;
  90. }
  91. jQuery.extend( jQuery.fn.dataTableExt.oSort, {
  92. "natural-asc": function ( a, b ) {
  93. return naturalSort(a,b,true);
  94. },
  95. "natural-desc": function ( a, b ) {
  96. return naturalSort(a,b,true) * -1;
  97. },
  98. "natural-nohtml-asc": function( a, b ) {
  99. return naturalSort(a,b,false);
  100. },
  101. "natural-nohtml-desc": function( a, b ) {
  102. return naturalSort(a,b,false) * -1;
  103. }
  104. } );
  105. }());