]>
jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blob - js/datatables-natural-sort.js
1 /*! © SpryMedia Ltd, Jim Palmer, Michael Buehler, Mike Grier, Clint Priest, Kyle Adams, guillermo - datatables.net/license */
4 if ( typeof define
=== 'function' && define
.amd
) {
6 define( ['jquery', 'datatables.net'], function ( $ ) {
7 return factory( $, window
, document
);
10 else if ( typeof exports
=== 'object' ) {
12 var jq
= require('jquery');
13 var cjsRequires = function (root
, $) {
14 if ( ! $.fn
.dataTable
) {
15 require('datatables.net')(root
, $);
19 if (typeof window
!== 'undefined') {
20 module
.exports = function (root
, $) {
22 // CommonJS environments without a window global must pass a
23 // root. This will give an error otherwise
31 cjsRequires( root
, $ );
32 return factory( $, root
, root
.document
);
36 cjsRequires( window
, jq
);
37 module
.exports
= factory( jq
, window
, window
.document
);
42 factory( jQuery
, window
, document
);
44 }(function( $, window
, document
, undefined ) {
46 var DataTable
= $.fn
.dataTable
;
50 * Data can often be a complicated mix of numbers and letters (file names
51 * are a common example) and sorting them in a natural manner is quite a
54 * Fortunately a deal of work has already been done in this area by other
55 * authors - the following plug-in uses the [naturalSort() function by Jim
56 * Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support) to provide natural sorting in DataTables.
58 * @name Natural sorting
59 * @summary Sort data with a mix of numbers and letters _naturally_.
60 * @author [Jim Palmer](http://www.overset.com/2008/09/01/javascript-natural-sort-algorithm-with-unicode-support)
61 * @author [Michael Buehler] (https://github.com/AnimusMachina)
64 * $('#example').dataTable( {
66 * { type: 'natural', targets: 0 }
70 * Html can be stripped from sorting by using 'natural-nohtml' such as
72 * $('#example').dataTable( {
74 * { type: 'natural-nohtml', targets: 0 }
80 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
81 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
82 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
83 * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
85 function naturalSort(a
, b
, html
) {
86 var re
= /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?%?$|^0x[0-9a-f]+$|[0-9]+)/gi, sre
= /(^[ ]*|[ ]*$)/g
, dre
= /(^([\w
]+,?[\w
]+)?[\w
]+,?[\w
]+\d
+:\d
+(:\d
+)?[\w
]?|^\d
{1,4}[\/\-]\d
{1,4}[\/\-]\d
{1,4}|^\w
+, \w
+ \d
+, \d
{4})/, hre
= /^0x[0-9a-f]+$/i, ore
= /^0/, htmre
= /(<([^>]+)>)/gi,
87 // convert all to strings and trim()
88 x
= a
.toString().replace(sre
, '') || '', y
= b
.toString().replace(sre
, '') || '';
89 // remove html from strings if desired
91 x
= x
.replace(htmre
, '');
92 y
= y
.replace(htmre
, '');
96 .replace(re
, '\0$1\0')
100 .replace(re
, '\0$1\0')
104 // numeric, hex or date detection
105 xD
= parseInt(x
.match(hre
), 10) ||
106 (xN
.length
!== 1 && x
.match(dre
) && Date
.parse(x
)), yD
= parseInt(y
.match(hre
), 10) ||
107 (xD
&& y
.match(dre
) && Date
.parse(y
)) ||
109 // first try and sort Hex codes or Dates
118 // natural sorting through split numeric strings and default strings
119 for (var cLoc
= 0, numS
= Math
.max(xN
.length
, yN
.length
); cLoc
< numS
; cLoc
++) {
120 // find floats not starting with '0', string or 0 if not defined (Clint Priest)
121 var oFxNcL
= (!(xN
[cLoc
] || '').match(ore
) && parseFloat(xN
[cLoc
])) || xN
[cLoc
] || 0;
122 var oFyNcL
= (!(yN
[cLoc
] || '').match(ore
) && parseFloat(yN
[cLoc
])) || yN
[cLoc
] || 0;
123 // handle numeric vs string comparison - number < string - (Kyle Adams)
124 if (isNaN(oFxNcL
) !== isNaN(oFyNcL
)) {
125 return isNaN(oFxNcL
) ? 1 : -1;
127 // rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
128 else if (typeof oFxNcL
!== typeof oFyNcL
) {
132 if (oFxNcL
< oFyNcL
) {
135 if (oFxNcL
> oFyNcL
) {
141 DataTable
.ext
.order
['natural-asc'] = function (a
, b
) {
142 return naturalSort(a
, b
, true);
144 DataTable
.ext
.order
['natural-desc'] = function (a
, b
) {
145 return naturalSort(a
, b
, true) * -1;
147 DataTable
.ext
.order
['natural-nohtml-asc'] = function (a
, b
) {
148 return naturalSort(a
, b
, false);
150 DataTable
.ext
.order
['natural-nohtml-asc'] = function (a
, b
) {
151 return naturalSort(a
, b
, false) * -1;
153 DataTable
.ext
.order
['natural-ci-asc'] = function (a
, b
) {
154 a
= a
.toString().toLowerCase();
155 b
= b
.toString().toLowerCase();
156 return naturalSort(a
, b
, true);
158 DataTable
.ext
.order
['natural-ci-asc'] = function (a
, b
) {
159 a
= a
.toString().toLowerCase();
160 b
= b
.toString().toLowerCase();
161 return naturalSort(a
, b
, true) * -1;