Update the sorting functions to take advantage of DataTables 1.9's preformatting option for sorting functions. Smaller code and faster execution - what's not to like!? Although do note that these sorting functions are not compatible with DataTables 1.8 and before (see previous commit if you do need compatiblity with older DataTables).

pull/2/head
Allan Jardine 13 years ago
parent 0bba914d8b
commit 2f64691d76

@ -7,17 +7,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"alt-string-asc": function( a, b ) { "alt-string-pre": function ( a ) {
var x = a.match(/alt="(.*?)"/)[1].toLowerCase(); return a.match(/alt="(.*?)"/)[1].toLowerCase();
var y = b.match(/alt="(.*?)"/)[1].toLowerCase(); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "alt-string-asc": function( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"alt-string-desc": function(a,b) { "alt-string-desc": function(a,b) {
var x = a.match(/alt="(.*?)"/)[1].toLowerCase(); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.match(/alt="(.*?)"/)[1].toLowerCase();
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -8,17 +8,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"anti-the-asc": function ( a, b ) { "anti-the-pre": function ( a ) {
var x = a.replace(/^the /i, ""); return a.replace(/^the /i, "");
var y = b.replace(/^the /i, ""); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "anti-the-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"anti-the-desc": function ( a, b ) { "anti-the-desc": function ( a, b ) {
var x = a.replace(/^the /i, ""); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.replace(/^the /i, "");
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -7,25 +7,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"currency-asc": function ( a, b ) { "currency-pre": function ( a ) {
/* Remove any formatting */ a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" ); return parseFloat( a );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" ); },
/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y; "currency-asc": function ( a, b ) {
return a - b;
}, },
"currency-desc": function ( a, b ) { "currency-desc": function ( a, b ) {
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" ); return b - a;
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return y - x;
} }
} ); } );

@ -6,55 +6,45 @@
* @author <a href="http://galjot.si/">Robert Sedovšek</a> * @author <a href="http://galjot.si/">Robert Sedovšek</a>
*/ */
(function(){
function calculate_date(date) {
var date = date.replace(" ", "");
if (date.indexOf('.') > 0) {
/*date a, format dd.mn.(yyyy) ; (year is optional)*/
var eu_date = date.split('.');
} else {
/*date a, format dd/mn/(yyyy) ; (year is optional)*/
var eu_date = date.split('/');
}
/*year (optional)*/
if (eu_date[2]) {
var year = eu_date[2];
} else {
var year = 0;
}
/*month*/
var month = eu_date[1];
if (month.length == 1) {
month = 0+month;
}
/*day*/
var day = eu_date[0];
if (day.length == 1) {
day = 0+day;
}
return (year + month + day) * 1;
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-eu-asc": function ( a, b ) { "date-eu-pre": function ( date ) {
x = calculate_date(a); var date = date.replace(" ", "");
y = calculate_date(b);
if (date.indexOf('.') > 0) {
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); /*date a, format dd.mn.(yyyy) ; (year is optional)*/
}, var eu_date = date.split('.');
} else {
"date-eu-desc": function ( a, b ) { /*date a, format dd/mn/(yyyy) ; (year is optional)*/
x = calculate_date(a); var eu_date = date.split('/');
y = calculate_date(b); }
return ((x < y) ? 1 : ((x > y) ? -1 : 0)); /*year (optional)*/
} if (eu_date[2]) {
var year = eu_date[2];
} else {
var year = 0;
}
/*month*/
var month = eu_date[1];
if (month.length == 1) {
month = 0+month;
}
/*day*/
var day = eu_date[0];
if (day.length == 1) {
day = 0+day;
}
return (year + month + day) * 1;
},
"date-eu-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"date-eu-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} ); } );
}());

@ -8,7 +8,7 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-euro-asc": function ( a, b ) { "date-euro-pre": function ( a ) {
if ($.trim(a) != '') { if ($.trim(a) != '') {
var frDatea = $.trim(a).split(' '); var frDatea = $.trim(a).split(' ');
var frTimea = frDatea[1].split(':'); var frTimea = frDatea[1].split(':');
@ -18,37 +18,14 @@
var x = 10000000000000; // = l'an 1000 ... var x = 10000000000000; // = l'an 1000 ...
} }
if ($.trim(b) != '') { return x;
var frDateb = $.trim(b).split(' ');
var frTimeb = frDateb[1].split(':');
frDateb = frDateb[0].split('/');
var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;
} else {
var y = 10000000000000;
}
var z = ((x < y) ? -1 : ((x > y) ? 1 : 0));
return z;
}, },
"date-euro-desc": function ( a, b ) { "date-euro-asc": function ( a, b ) {
if ($.trim(a) != '') { return a - b;
var frDatea = $.trim(a).split(' '); },
var frTimea = frDatea[1].split(':');
var frDatea2 = frDatea[0].split('/');
var x = (frDatea2[2] + frDatea2[1] + frDatea2[0] + frTimea[0] + frTimea[1] + frTimea[2]) * 1;
} else {
var x = 10000000000000;
}
if ($.trim(b) != '') { "date-euro-desc": function ( a, b ) {
var frDateb = $.trim(b).split(' '); return b - a;
var frTimeb = frDateb[1].split(':');
frDateb = frDateb[0].split('/');
var y = (frDateb[2] + frDateb[1] + frDateb[0] + frTimeb[0] + frTimeb[1] + frTimeb[2]) * 1;
} else {
var y = 10000000000000;
}
var z = ((x < y) ? 1 : ((x > y) ? -1 : 0));
return z;
} }
} ); } );

@ -10,23 +10,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"date-uk-asc": function ( a, b ) { "date-uk-pre": function ( a ) {
var ukDatea = a.split('/'); var ukDatea = a.split('/');
var ukDateb = b.split('/'); return (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
},
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "date-uk-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"date-uk-desc": function ( a, b ) { "date-uk-desc": function ( a, b ) {
var ukDatea = a.split('/'); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var ukDateb = b.split('/');
var x = (ukDatea[2] + ukDatea[1] + ukDatea[0]) * 1;
var y = (ukDateb[2] + ukDateb[1] + ukDateb[0]) * 1;
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -7,32 +7,22 @@
* @author <a href="http://sprymedia.co.uk">Allan Jardine</a> * @author <a href="http://sprymedia.co.uk">Allan Jardine</a>
*/ */
(function() {
function priority( a ) {
// Add / alter the switch statement below to match your enum list
switch( a ) {
case "High": return 1;
case "Medium": return 2;
case "Low": return 3;
default: return 4;
}
}
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"enum-asc": function ( a, b ) { "enum-pre": function ( a ) {
var x = priority( a ); // Add / alter the switch statement below to match your enum list
var y = priority( b ); switch( a ) {
case "High": return 1;
case "Medium": return 2;
case "Low": return 3;
default: return 4;
}
},
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "enum-asc": function ( a, b ) {
}, return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"enum-desc": function ( a, b ) { "enum-desc": function ( a, b ) {
var x = priority( a ); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = priority( b ); }
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
} ); } );
}());

@ -9,33 +9,20 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"file-size-asc": function ( a, b ) { "file-size-pre": function ( a ) {
var x = a.substring(0,a.length - 2); var x = a.substring(0,a.length - 2);
var y = b.substring(0,b.length - 2);
var x_unit = (a.substring(a.length - 2, a.length) == "MB" ? var x_unit = (a.substring(a.length - 2, a.length) == "MB" ?
1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1)); 1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1));
var y_unit = (b.substring(b.length - 2, b.length) == "MB" ?
1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1));
x = parseInt( x * x_unit ); return parseInt( x * x_unit, 10 );
y = parseInt( y * y_unit ); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "file-size-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"file-size-desc": function ( a, b ) { "file-size-desc": function ( a, b ) {
var x = a.substring(0,a.length - 2); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.substring(0,b.length - 2);
var x_unit = (a.substring(a.length - 2, a.length) == "MB" ?
1000 : (a.substring(a.length - 2, a.length) == "GB" ? 1000000 : 1));
var y_unit = (b.substring(b.length - 2, b.length) == "MB" ?
1000 : (b.substring(b.length - 2, b.length) == "GB" ? 1000000 : 1));
x = parseInt( x * x_unit);
y = parseInt( y * y_unit);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -8,9 +8,9 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"ip-address-asc": function ( a, b ) { "ip-address-pre": function ( a ) {
var m = a.split("."), x = ""; var m = a.split("."), x = "";
var n = b.split("."), y = "";
for(var i = 0; i < m.length; i++) { for(var i = 0; i < m.length; i++) {
var item = m[i]; var item = m[i];
if(item.length == 1) { if(item.length == 1) {
@ -21,42 +21,15 @@ jQuery.extend( jQuery.fn.dataTableExt.oSort, {
x += item; x += item;
} }
} }
for(var i = 0; i < n.length; i++) {
var item = n[i]; return x;
if(item.length == 1) { },
y += "00" + item;
} else if(item.length == 2) { "ip-address-asc": function ( a, b ) {
y += "0" + item; return ((a < b) ? -1 : ((a > b) ? 1 : 0));
} else {
y += item;
}
}
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}, },
"ip-address-desc": function ( a, b ) { "ip-address-desc": function ( a, b ) {
var m = a.split("."), x = ""; return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var n = b.split("."), y = "";
for(var i = 0; i < m.length; i++) {
var item = m[i];
if(item.length == 1) {
x += "00" + item;
} else if (item.length == 2) {
x += "0" + item;
} else {
x += item;
}
}
for(var i = 0; i < n.length; i++) {
var item = n[i];
if(item.length == 1) {
y += "00" + item;
} else if (item.length == 2) {
y += "0" + item;
} else {
y += item;
}
}
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -8,15 +8,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"monthYear-pre": function ( a ) {
return new Date('01 '+a);
},
"monthYear-asc": function ( a, b ) { "monthYear-asc": function ( a, b ) {
a = new Date('01 '+a); return ((a < b) ? -1 : ((a > b) ? 1 : 0));
b = new Date('01 '+b);
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"monthYear-desc": function ( a, b ) { "monthYear-desc": function ( a, b ) {
a = new Date('01 '+a);
b = new Date('01 '+b);
return ((a < b) ? 1 : ((a > b) ? -1 : 0)); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
} }
} ); } );

@ -9,21 +9,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"num-html-asc": function ( a, b ) { "num-html-pre": function ( a ) {
var x = a.replace( /<.*?>/g, "" ); var x = a.replace( /<.*?>/g, "" );
var y = b.replace( /<.*?>/g, "" ); return parseFloat( x );
x = parseFloat( x ); },
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "num-html-asc": function ( a, b ) {
}, return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},
"num-html-desc": function ( a, b ) { "num-html-desc": function ( a, b ) {
var x = a.replace( /<.*?>/g, "" ); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.replace( /<.*?>/g, "" ); }
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
} ); } );

@ -8,21 +8,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"numeric-comma-asc": function ( a, b ) { "numeric-comma-pre": function ( a ) {
var x = (a == "-") ? 0 : a.replace( /,/, "." ); var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." ); return parseFloat( x );
x = parseFloat( x ); },
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "numeric-comma-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"numeric-comma-desc": function ( a, b ) { "numeric-comma-desc": function ( a, b ) {
var x = (a == "-") ? 0 : a.replace( /,/, "." ); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -5,21 +5,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"percent-asc": function ( a, b ) { "percent-pre": function ( a ) {
var x = (a == "-") ? 0 : a.replace( /%/, "" ); var x = (a == "-") ? 0 : a.replace( /%/, "" );
var y = (b == "-") ? 0 : b.replace( /%/, "" ); return parseFloat( x );
x = parseFloat( x ); },
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "percent-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"percent-desc": function ( a, b ) { "percent-desc": function ( a, b ) {
var x = (a == "-") ? 0 : a.replace( /%/, "" ); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = (b == "-") ? 0 : b.replace( /%/, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -32,18 +32,16 @@ function GetUniCode(source) {
}; };
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"pstring-asc": function ( a, b ) { "pstring-pre": function ( a ) {
var x = GetUniCode(a.toLowerCase()); return GetUniCode(a.toLowerCase());
var y = GetUniCode(b.toLowerCase()); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "pstring-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"pstring-desc": function ( a, b ) { "pstring-desc": function ( a, b ) {
var x = GetUniCode(a.toLowerCase()); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = GetUniCode(b.toLowerCase());
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -6,17 +6,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"scientific-asc": function ( a, b ) { "scientific-pre": function ( a ) {
var x = parseFloat(a); return parseFloat(a);
var y = parseFloat(b); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "scientific-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"scientific-desc": function ( a, b ) { "scientific-desc": function ( a, b ) {
var x = parseFloat(a); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = parseFloat(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -7,15 +7,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"signed-num-pre": function ( a ) {
return (a=="-" || a==="") ? 0 : a.replace('+','')*1;
},
"signed-num-asc": function ( a, b ) { "signed-num-asc": function ( a, b ) {
var x = (a=="-" || a==="") ? 0 : a.replace('+','')*1; return ((a < b) ? -1 : ((a > b) ? 1 : 0));
var y = (b=="-" || b==="") ? 0 : b.replace('+','')*1;
return x - y;
}, },
"signed-num-desc": function ( a, b ) { "signed-num-desc": function ( a, b ) {
var x = (a=="-" || a==="") ? 0 : a.replace('+','')*1; return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = (b=="-" || b==="") ? 0 : b.replace('+','')*1;
return y - x;
} }
} ); } );

@ -11,21 +11,16 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"title-numeric-asc": function ( a, b ) { "title-numeric-pre": function ( a ) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1]; var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1]; return parseFloat( x );
x = parseFloat( x ); },
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "title-numeric-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"title-numeric-desc": function ( a, b ) { "title-numeric-desc": function ( a, b ) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1]; return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

@ -8,17 +8,15 @@
*/ */
jQuery.extend( jQuery.fn.dataTableExt.oSort, { jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"title-string-asc": function ( a, b ) { "title-string-pre": function ( a ) {
var x = a.match(/title="(.*?)"/)[1].toLowerCase(); return a.match(/title="(.*?)"/)[1].toLowerCase();
var y = b.match(/title="(.*?)"/)[1].toLowerCase(); },
return ((x < y) ? -1 : ((x > y) ? 1 : 0)); "title-string-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}, },
"title-string-desc": function ( a, b ) { "title-string-desc": function ( a, b ) {
var x = a.match(/title="(.*?)"/)[1].toLowerCase(); return ((a < b) ? 1 : ((a > b) ? -1 : 0));
var y = b.match(/title="(.*?)"/)[1].toLowerCase();
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
} }
} ); } );

Loading…
Cancel
Save