Sorting numbers with spaces
Sorting numbers with spaces
dongi_dong
Posts: 1Questions: 0Answers: 0
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!
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!
This discussion has been closed.
Replies
Allan