# sorting time in d:h:m:s format

# sorting time in d:h:m:s format

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]

This discussion has been closed.

## Replies

Allan