Numeric/Currency Sorting not sorting properly or i am doing something wrong
Numeric/Currency Sorting not sorting properly or i am doing something wrong
The following explains my sorting problem. It shows how it is now and how I tried to fix it. I can't provide a link because it's an intranet development. I am new to datatables and appreciate any help. I have found datatable ideal so far for this project and look forward to participating financially and with user input.
Here it goes
The sorting when selected on my columns looks like this:
1,600
1,700
10
10
20
12,816
12,816
I am using
jquery.dataTables.js (latest version)
DT_bootstrap.js
After I apply the numeric sort and currency plugins the sort looks like this:
2,021
3,517
4,021
4,032
6,772
10
16
20
23,235
24
....
80
85
88,235
It seems to sort better but it is having problems with the third digit after the comma
My setup is using
jquery.dataTables.js (latest version)
DT_bootstrap.js
with these plugins and initialisation setup
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"currency-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},
"currency-asc": function ( a, b ) {
return a - b;
},
"currency-desc": function ( a, b ) {
return b - a;
}
} );
jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
/* Table initialisation */
$(document).ready(function() {
$('#example01').dataTable( {
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'>>",
"sPaginationType": "bootstrap",
"iDisplayLength": 25,
"bFilter": true,
"bSort" : true,
"aoColumns": [
null,
null,
null,
null,
{ "sType": "numeric-comma" },
{ "sType": "numeric-comma" },
{ "sType": "numeric-comma" },
null,
null
],
"oLanguage": {
"sLengthMenu": ''+
'10'+
'25'+
'50'+
'100'+
'All'+
''
}
} );
} );
Here it goes
The sorting when selected on my columns looks like this:
1,600
1,700
10
10
20
12,816
12,816
I am using
jquery.dataTables.js (latest version)
DT_bootstrap.js
After I apply the numeric sort and currency plugins the sort looks like this:
2,021
3,517
4,021
4,032
6,772
10
16
20
23,235
24
....
80
85
88,235
It seems to sort better but it is having problems with the third digit after the comma
My setup is using
jquery.dataTables.js (latest version)
DT_bootstrap.js
with these plugins and initialisation setup
jQuery.extend( jQuery.fn.dataTableExt.oSort, {
"currency-pre": function ( a ) {
a = (a==="-") ? 0 : a.replace( /[^\d\-\.]/g, "" );
return parseFloat( a );
},
"currency-asc": function ( a, b ) {
return a - b;
},
"currency-desc": function ( a, b ) {
return b - a;
}
} );
jQuery.fn.dataTableExt.oSort['numeric-comma-asc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/, "." );
var y = (b == "-") ? 0 : b.replace( /,/, "." );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
/* Table initialisation */
$(document).ready(function() {
$('#example01').dataTable( {
"sDom": "<'row'<'span6'l><'span6'f>r>t<'row'<'span6'i><'span6'>>",
"sPaginationType": "bootstrap",
"iDisplayLength": 25,
"bFilter": true,
"bSort" : true,
"aoColumns": [
null,
null,
null,
null,
{ "sType": "numeric-comma" },
{ "sType": "numeric-comma" },
{ "sType": "numeric-comma" },
null,
null
],
"oLanguage": {
"sLengthMenu": ''+
'10'+
'25'+
'50'+
'100'+
'All'+
''
}
} );
} );
This discussion has been closed.
Replies
If this helps I placed a sample of the page here.
http://pranger.com/datatables/test-table01.html
the js file DT_numericSort.js contains the plugins
From an earlier post
1. I replaced this part of the numeric-comma plugin in DT_numericSort.js
a.replace( /,/, "." );
b.replace( /,/, "." );
to
a.replace( /,/g, "" );
b.replace( /,/g, "" );
for both the asc and desc
2. added the sType for the last column from null to
"sType": "currency"
(this was me being dumb)
The currrency plugin didn't change .
3. Replaced any negative data with ( ) brackets with a negative sign and no space.
So you can see the change at:
http://pranger.com/datatables/test-table01.html
i need aocoloumns dynamic how to set it sir because sometimes currency comes in second or third coloumn how to set it sir
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
var sValidChars = "0123456789-,";
var Char;
var bDecimal = false;
/* Check the numeric part */
for ( i=0 ; i y) ? 1 : 0));
};
jQuery.fn.dataTableExt.oSort['numeric-comma-desc'] = function(a,b) {
var x = (a == "-") ? 0 : a.replace( /,/g, "" );
var y = (b == "-") ? 0 : b.replace( /,/g, "" );
x = parseFloat( x );
y = parseFloat( y );
return ((x < y) ? 1 : ((x > y) ? -1 : 0));
};
$(document).ready(function() {
oTable = $('#example').dataTable({
"bJQueryUI": true,
"bPaginate": false,
"aaSorting": [[ 1, "desc" ]]
});
got answer