Skip to content

Instantly share code, notes, and snippets.

@jonperron
Created November 16, 2016 11:18
Show Gist options
  • Save jonperron/ce8ac46d2855289386049420d01f5a7c to your computer and use it in GitHub Desktop.
Save jonperron/ce8ac46d2855289386049420d01f5a7c to your computer and use it in GitHub Desktop.
Jquery Datatables (https://datatables.net/) got a plugin to sort durations. Unfortunately, it does not work when the duration is very long (230:59:59 as an example). This is how I did it, based on http://stackoverflow.com/questions/30946698/custom-sort-durations-using-jquery-datatables. Original SO : http://stackoverflow.com/questions/40285331/s…
$.extend(jQuery.fn.dataTableExt.oSort, {
"duration-pre": function (s) {
var duration;
s = s.toLowerCase();
if(s === 'n/a'){
duration = -1;
} else {
d = s.match(/(?:(\d+):)?(?:(\d+):)?(?:(\d+):)?/);
duration = parseInt(d[1] ? d[1] : 0) * 60 + parseInt(d[2] ? d[2] : 0) + parseInt(d[2] ? d[2] : 0) / 60;
}
return duration;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment