Datatable sorting scientific notation
Datatable sorting scientific notation
vivekmyself
Posts: 16Questions: 0Answers: 0
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?
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?
This discussion has been closed.
Replies
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
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
[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.
Allan
Thanks very much for contributing to the project!
Regards,
Allan