Currency Sorting Issue

Currency Sorting Issue

codycody Posts: 1Questions: 0Answers: 0
edited January 2012 in Plug-ins
Hello, I've had a issue with the currency in my table which is a similar issue to the following post.

The exact issue is where the numbers are out of order or just wont sort:

$6.00
$500.00
$500.00
$50.00
$5.00
$5.00
$5.00
$4,072.66
$10,634,884.55

Empty rows have '--' so sometimes it will do

--
$6.00
$500.00
$500.00
$50.00
$5.00
--
$5.00
$5.00
$4,072.66
--
$10,634,884.55
--
--

Does anyone have any ideas why this would be happening?

http://datatables.net/forums/discussion/549/cant-get-currency-sorting-to-work/p1

The solutions in that post did not help.

Here is the code:

[code]
$.fn.dataTableExt.oSort['currency-asc'] = function(a,b) {
/* Remove any formatting */
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );

/* Parse and return */
x = parseFloat( x );
y = parseFloat( y );
return x - y;
};

$.fn.dataTableExt.oSort['currency-desc'] = function(a,b) {
var x = a == "-" ? 0 : a.replace( /[^\d\-\.]/g, "" );
var y = b == "-" ? 0 : b.replace( /[^\d\-\.]/g, "" );

x = parseFloat( x );
y = parseFloat( y );
return y - x;
};

$(document).ready(function(){

TableTools.DEFAULTS.aButtons = [ "copy", "csv", "xls", "pdf", "print" ];

$('#table').dataTable({
"sDom": "<'row'<'span4'l><'span6'T><'span6'f>r>t<'row'<'span8'i><'span8'p>>",
"sPaginationType": "bootstrap",
"iDisplayLength": 25,
"aLengthMenu": [[25, 50, 100, 250, -1], [25, 50, 100, 250, "All"]],
"aaSorting": [],
"oTableTools": {
"sSwfPath": "js/copy_cvs_xls_pdf.swf"
},
"aoColumns": [
{ "bSortable": false },
null,
null,
null,
null,
{ "sType": "currency" },
{ "sType": "numeric" },
{ "sType": "currency" },
{ "sType": "numeric" },
null
]
});
$.extend( $.fn.dataTableExt.oStdClasses, {
"sSortAsc": "header headerSortDown",
"sSortDesc": "header headerSortUp",
"sSortable": "header"
} );

});
[/code]

Thank you

Replies

  • allanallan Posts: 61,650Questions: 1Answers: 10,094 Site admin
    The "--" in the column is very certainly going to upset the plug-in, since there is no handling for that. If you have a look at how x and y are defined in the plug-in you'll see handling for "-", I think you just need to alter that to deal with "--" (i.e. add an extra dash).

    Failing that, a link to a page showing the problem would be good, a link to a http://live.datatables.net demo showing the problem would be ideal!

    Allan
This discussion has been closed.