sorting time in d:h:m:s format

sorting time in d:h:m:s format

tekwitekwi Posts: 1Questions: 0Answers: 0
edited February 2014 in Plug-ins
I was looking for a oSort funtion in dataTables to sort date which is in this format : 20d:20h:21:m:4s

I thought I would share here for others who are looking for something similar.
[code]
/*
sort by time in days hrs min sec (d:h:m:s)
*/
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"time-since-pre": function ( a ) {
var x = new Array();
var sec = 0;
if(a.indexOf(':') !== -1){
x = a.split(':');
}else{ x[0] = a;}
for (var i = 0; i < x.length; i++) {
sec += parseInt(x[i].substring(x[i].length - 1, x[i].length) == 'd'?
(x[i].substring(0, x[i].length - 1)* 24*60*60):(x[i].substring(x[i].length - 1, x[i].length) == 'h'? (x[i].substring(0, x[i].length - 1)* 60*60):(x[i].substring(x[i].length - 1, x[i].length) == 'm'? (x[i].substring(0, x[i].length - 1)* 60):(x[i].substring(x[i].length - 1, x[i].length) == 's'? (x[i].substring(0, x[i].length - 1)):''))));
}
return parseInt(sec);
},

"time-since-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"time-since-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );
[/code]

Replies

  • allanallan Posts: 63,691Questions: 1Answers: 10,500 Site admin
    Nice one - thanks very much for sharing this with us :-)

    Allan
This discussion has been closed.