Sorting a column numerically that includes a unit of measure
Sorting a column numerically that includes a unit of measure
smsorensen
Posts: 8Questions: 0Answers: 0
How do I set a column to sort numerically with a unit of measure? My data is passing back a number and I am adding "mi.".
var distanceCombo = distance + " mi.";
loadRowArray.push(distanceCombo);
...
loadDataArray.push(loadRowArray);
...
shipmentSearchResultsTable.fnAddData(loadDataArray);
This discussion has been closed.
Replies
http://datatables.net/plug-ins/sorting
http://datatables.net/release-datatables/examples/plug-ins/sorting_sType.html
All it needs to do is remove the " mi." and parse as an int.
Allan
jQuery.fn.dataTableExt.oSort['numeric-mile-asc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /mi./, "" );
var y = (b == "-") ? 0 : b.replace( /mi./, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-mile-desc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /mi./, "" );
var y = (b == "-") ? 0 : b.replace( /mi./, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};