Sorting numbers with spaces

Sorting numbers with spaces

dongi_dongdongi_dong Posts: 1Questions: 0Answers: 0
edited August 2012 in General
Hi,
I have a problem with sorting my table. In one column I have a numbers formatted e.g "12 345 678" which I want to sort as a numerical fields. However they are sorted as strings.

My code:

index.html
[code]






...



First
Second




1
23 300


2
9 992



[/code]

custom.js
[code]
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"formatted-num-pre": function ( a ) {
var x = a.replace( / /g, '' );
return parseFloat( x );
},

"formatted-num-asc": function ( a, b ) {
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
},

"formatted-num-desc": function ( a, b ) {
return ((a < b) ? 1 : ((a > b) ? -1 : 0));
}
} );

oTable = $('.dTable').dataTable({
"bJQueryUI": true,
"bAutoWidth": false,
"sPaginationType": "full_numbers",
"sDom": '<"H"l>t<"F"fp>',
"aoColumns": [
null,
{ "sType": 'formatted-num' }
]
});
[/code]

I have also tried with adding this code:
custom.js
[code]
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
var deformatted = sData.replace(/ /g,'');
if ( $.isNumeric( deformatted ) || deformatted === "-" ) {
return 'formatted-num';
}
return null;
}
);
[/code]

Always the second column was sorted as a string.

Any ideas?

Thanks in advance!

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    edited August 2012
    Can you link us to a test case showing the problem please? That looks like it should work. Or at least running it through the DataTables debugger :-)

    Allan
This discussion has been closed.