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 32(function() { 33 34/* 35 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license 36 * Author: Jim Palmer (based on chunking idea from Dave Koelle) 37 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo 38 * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js 39 */ 40function naturalSort (a, b, html) { 41 var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi, 42 sre = /(^[ ]*|[ ]*$)/g, 43 dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/, 44 hre = /^0x[0-9a-f]+$/i, 45 ore = /^0/, 46 htmre = /(<([^>]+)>)/ig, 47 // convert all to strings and trim() 48 x = a.toString().replace(sre, '') || '', 49 y = b.toString().replace(sre, '') || ''; 50 // remove html from strings if desired 51 if (!html) { 52 x = x.replace(htmre, ''); 53 y = y.replace(htmre, ''); 54 } 55 // chunk/tokenize 56 var xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 57 yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'), 58 // numeric, hex or date detection 59 xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)), 60 yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null; 61 62 // first try and sort Hex codes or Dates 63 if (yD) { 64 if ( xD < yD ) { 65 return -1; 66 } 67 else if ( xD > yD ) { 68 return 1; 69 } 70 } 71 72 // natural sorting through split numeric strings and default strings 73 for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) { 74 // find floats not starting with '0', string or 0 if not defined (Clint Priest) 75 var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc], 10) || xN[cLoc] || 0; 76 var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc], 10) || yN[cLoc] || 0; 77 // handle numeric vs string comparison - number < string - (Kyle Adams) 78 if (isNaN(oFxNcL) !== isNaN(oFyNcL)) { 79 return (isNaN(oFxNcL)) ? 1 : -1; 80 } 81 // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2' 82 else if (typeof oFxNcL !== typeof oFyNcL) { 83 oFxNcL += ''; 84 oFyNcL += ''; 85 } 86 if (oFxNcL < oFyNcL) { 87 return -1; 88 } 89 if (oFxNcL > oFyNcL) { 90 return 1; 91 } 92 } 93 return 0; 94} 95 96jQuery.extend( jQuery.fn.dataTableExt.oSort, { 97 "natural-asc": function ( a, b ) { 98 return naturalSort(a,b,true); 99 }, 100 101 "natural-desc": function ( a, b ) { 102 return naturalSort(a,b,true) * -1; 103 }, 104 105 "natural-nohtml-asc": function( a, b ) { 106 return naturalSort(a,b,false); 107 }, 108 109 "natural-nohtml-desc": function( a, b ) { 110 return naturalSort(a,b,false) * -1; 111 } 112} ); 113 114}());
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.