Datatable sorting scientific notation

Datatable sorting scientific notation

vivekmyselfvivekmyself Posts: 16Questions: 0Answers: 0
edited July 2011 in Plug-ins
Hi,

I am using dataTables to display the content. I am not able to sort with scientific notation. Datatable is treating p-values as string when in scientific notation (1E-10). It should be treating it as number. Is there any plugin to sort the scientific notations?

Replies

  • nickschurchnickschurch Posts: 18Questions: 0Answers: 0
    I'm having this issue too. Anyone know of a good way to make dataTables sort Scientific notation properly? Ideally it should be able to handle sorting a column containing a mixture of numerical formats. So...

    6.5, 5, 5.4E2, 1.2E6, 0.000001, 1E-2

    should sort as:

    0.000001, 1E-2, 5, 6.5, 5.4E2, 1.2E6
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    A sorting plug-in would be needed for this since they aren't read as numbers by Javascript. There is documentation on how to do it here:
    http://datatables.net/development/sorting

    and a number of sorting plug-ins already built (although not one that matches your requirements I'm afraid) here:
    http://datatables.net/plug-ins/sorting

    If you do create such a plug-in, it would be great if I could add it to the plug-ins page!

    Allan
  • nickschurchnickschurch Posts: 18Questions: 0Answers: 0
    edited January 2012
    Thanks for pointing me to that lot, its actually trivially easy! parseFloat turns out to be rather clever...

    [code]
    /* new sorting functions */
    jQuery.fn.dataTableExt.oSort['allnumeric-asc'] = function(a,b) {
    var x = parseFloat(a);
    var y = parseFloat(b);
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    };

    jQuery.fn.dataTableExt.oSort['allnumeric-desc'] = function(a,b) {
    var x = parseFloat(a);
    var y = parseFloat(b);
    return ((x < y) ? 1 : ((x > y) ? -1 : 0));
    };

    /* pick the column to give the datatype 'allnumeric' too */
    $('#example').dataTable({
    "aoColumnDefs": [{ "sType": "allnumeric", "aTargets": [ 3 ] } ]
    } );
    [/code]

    This stupidly straightforward solution seems to work just fine.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Perfect! I didn't know parseFloat could do that :-). Can I post it up not eh plug-ins page? What name / link would you like for the credit?

    Allan
  • nickschurchnickschurch Posts: 18Questions: 0Answers: 0
    Sure, no problem. My name is Nick Schurch and feel free to link it to my forum user id.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Super thanks! I've put it up here: http://datatables.net/plug-ins/sorting#scientific (called it 'scientific' rather than 'allnumeric' as I think in future it will be easier for people to find when they are looking for just such a function, but otherwise identical :-) ).

    Thanks very much for contributing to the project!

    Regards,
    Allan
  • nickschurchnickschurch Posts: 18Questions: 0Answers: 0
    No problem, I've taken far more from it than I've contributed!
This discussion has been closed.