I am using jquery.datatables to show numbers in datatables columns. Numbers are formatted to have spaces in between thousands unit (like 123 456 789
). Unfortunately, this number formatting provoques a string sorting instead of a number sorting (see the screenshot at the end of this question).
I have identified that:
function _fnSort(oSettings, bApplyClasses) {
is the core function for sorting.if (!window.runtime) {
is true)The string sorting functions used are the two following functions.
/*
* text sorting
*/
"string-asc": function(a, b) {
var x = a.toLowerCase();
var y = b.toLowerCase();
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
},
"string-desc": function(a, b) {
var x = a.toLowerCase();
var y = b.toLowerCase();
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
},
My knowledge in javascript is pretty poor, what would be the best approach here?
Here is what the sorting look like by now:
To sort this kind of values, you can use this sort function :
var sortFunction=function(a, b){
var ia = parseInt(a.split(' ').join(''), 10);
var ib = parseInt(b.split(' ').join(''), 10);
return ia-ib;
};
Test :
var data = ['3 333', '100 333', '22 000', '1 333'];
console.log(data.sort(sortFunction));
With a reasonable number of values, this will be fast enough. You shouldn't try to enrich the data if you don't detect performance problems.
EDIT :
In fact, the documentation proposes an appropriate (similar) sorting function :
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted_numbers-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},
"formatted_numbers-asc": function ( a, b ) {
return a - b;
},
"formatted_numbers-desc": function ( a, b ) {
return b - a;
}
} );
After having added this extension, you just have to set the sType
of your column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With