Subversion-Projekte lars-tiefland.cienc

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
8 lars 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
 *
14
 *  @example
15
 *    $('#example').dataTable( {
16
 *       columnDefs: [
17
 *         { type: 'natural', targets: 0 }
18
 *       ]
19
 *    } );
20
 */
21
 
22
(function() {
23
 
24
/*
25
 * Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license
26
 * Author: Jim Palmer (based on chunking idea from Dave Koelle)
27
 * Contributors: Mike Grier (mgrier.com), Clint Priest, Kyle Adams, guillermo
28
 * See: http://js-naturalsort.googlecode.com/svn/trunk/naturalSort.js
29
 */
30
function naturalSort (a, b) {
31
	var re = /(^-?[0-9]+(\.?[0-9]*)[df]?e?[0-9]?$|^0x[0-9a-f]+$|[0-9]+)/gi,
32
		sre = /(^[ ]*|[ ]*$)/g,
33
		dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/,
34
		hre = /^0x[0-9a-f]+$/i,
35
		ore = /^0/,
36
		// convert all to strings and trim()
37
		x = a.toString().replace(sre, '') || '',
38
		y = b.toString().replace(sre, '') || '',
39
		// chunk/tokenize
40
		xN = x.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
41
		yN = y.replace(re, '\0$1\0').replace(/\0$/,'').replace(/^\0/,'').split('\0'),
42
		// numeric, hex or date detection
43
		xD = parseInt(x.match(hre), 10) || (xN.length !== 1 && x.match(dre) && Date.parse(x)),
44
		yD = parseInt(y.match(hre), 10) || xD && y.match(dre) && Date.parse(y) || null;
45
 
46
	// first try and sort Hex codes or Dates
47
	if (yD) {
48
		if ( xD < yD ) {
49
			return -1;
50
		}
51
		else if ( xD > yD )	{
52
			return 1;
53
		}
54
	}
55
 
56
	// natural sorting through split numeric strings and default strings
57
	for(var cLoc=0, numS=Math.max(xN.length, yN.length); cLoc < numS; cLoc++) {
58
		// find floats not starting with '0', string or 0 if not defined (Clint Priest)
59
		var oFxNcL = !(xN[cLoc] || '').match(ore) && parseFloat(xN[cLoc], 10) || xN[cLoc] || 0;
60
		var oFyNcL = !(yN[cLoc] || '').match(ore) && parseFloat(yN[cLoc], 10) || yN[cLoc] || 0;
61
		// handle numeric vs string comparison - number < string - (Kyle Adams)
62
		if (isNaN(oFxNcL) !== isNaN(oFyNcL)) {
63
			return (isNaN(oFxNcL)) ? 1 : -1;
64
		}
65
		// rely on string comparison if different types - i.e. '02' < 2 != '02' < '2'
66
		else if (typeof oFxNcL !== typeof oFyNcL) {
67
			oFxNcL += '';
68
			oFyNcL += '';
69
		}
70
		if (oFxNcL < oFyNcL) {
71
			return -1;
72
		}
73
		if (oFxNcL > oFyNcL) {
74
			return 1;
75
		}
76
	}
77
	return 0;
78
}
79
 
80
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
81
	"natural-asc": function ( a, b ) {
82
		return naturalSort(a,b);
83
	},
84
 
85
	"natural-desc": function ( a, b ) {
86
		return naturalSort(a,b) * -1;
87
	}
88
} );
89
 
90
}());