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, {
"alt-string-asc": function( a, b ) {
var x = a.match(/alt="(.*?)"/)[1].toLowerCase();
var y = b.match(/alt="(.*?)"/)[1].toLowerCase();
"alt-string-pre": function ( a ) {
return a.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) {
var x = a.match(/alt="(.*?)"/)[1].toLowerCase();
var y = b.match(/alt="(.*?)"/)[1].toLowerCase();
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

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

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

@ -6,55 +6,45 @@
* @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, {
"date-eu-asc": function ( a, b ) {
x = calculate_date(a);
y = calculate_date(b);
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
"date-eu-desc": function ( a, b ) {
x = calculate_date(a);
y = calculate_date(b);
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
"date-eu-pre": function ( 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;
},
"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, {
"date-euro-asc": function ( a, b ) {
"date-euro-pre": function ( a ) {
if ($.trim(a) != '') {
var frDatea = $.trim(a).split(' ');
var frTimea = frDatea[1].split(':');
@ -18,37 +18,14 @@
var x = 10000000000000; // = l'an 1000 ...
}
if ($.trim(b) != '') {
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;
return x;
},
"date-euro-desc": function ( a, b ) {
if ($.trim(a) != '') {
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;
}
"date-euro-asc": function ( a, b ) {
return a - b;
},
if ($.trim(b) != '') {
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 ) {
return b - a;
}
} );

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

@ -7,32 +7,22 @@
* @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, {
"enum-asc": function ( a, b ) {
var x = priority( a );
var y = priority( b );
"enum-pre": function ( 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;
}
},
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 ) {
var x = priority( a );
var y = priority( b );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
}
"enum-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
}());

@ -9,33 +9,20 @@
*/
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 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 parseInt( x * x_unit, 10 );
},
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 ) {
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" ?
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));
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

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

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

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

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

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

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

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

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

@ -11,21 +11,16 @@
*/
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 y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return parseFloat( x );
},
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 ) {
var x = a.match(/title="*(-?[0-9\.]+)/)[1];
var y = b.match(/title="*(-?[0-9\.]+)/)[1];
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

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

Loading…
Cancel
Save