Sorting a column numerically that includes a unit of measure

Sorting a column numerically that includes a unit of measure

smsorensensmsorensen Posts: 8Questions: 0Answers: 0
edited June 2011 in General

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);

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    You will need a custom sorting plug-in to sort on your special data:

    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
  • smsorensensmsorensen Posts: 8Questions: 0Answers: 0
    Tried this, but doesn't seem to be working:

    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));
    };
This discussion has been closed.