Column sorting in IE vs. Firefox

Column sorting in IE vs. Firefox

amithaniamithani Posts: 4Questions: 0Answers: 0
edited February 2011 in General
Hi Allan,

I have a table with about 7 columns, 2 of them are string and 5 of them are currency. I wrote a custom data detection function and custom sorting function for Currency and it works fine in all browsers as long as there are no negative numbers in the column. One of the columns in the table has negative numbers and the sorting fails in IE and Chrome, while it works as expected in Firefox.

Here is datatype detection code:
[code]

/*Function to automatically detect the data type of a datatable column.*/
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
/*Removing HTML, comma and space from the input data*/
sData = String(sData).replace(/<[^>]*>?/g, "").replace(/ /g, "").replace(/,/g, "");
sData = String(sData).replace( /<.*?>/g, "" );

if (String(sData).match(/^[-]?\$\d*[.]?\d*$/))
{
return 'currency';
}
}
);
[/code]

Following is the sorting code:
[code]

/***
* Function to sort datatable's currency columns in ascending order.
*/
jQuery.fn.dataTableExt.oSort['currency-asc'] = function(a,b)
{
/*Removing HTML, comma and space from the input data*/
a = String(a).replace(/<[^>]*>?/g, "").replace(/ /g, "").replace(/,/g, "");
b = String(b).replace(/<[^>]*>?/g, "").replace(/ /g, "").replace(/,/g, "");

/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
if(b.substring( 1, 0 ) == '-')
{
/* Remove the currency sign, after the minus! */
var x = a == "-" ? 0 : a.replace( new RegExp(/[^-0-9.]/g), "" );
var y = b == "-" ? 0 : b.replace( new RegExp(/[^-0-9.]/g), "" );
x = x.substring( 1, 0 ) + x.substring( 1 );
y = y.substring( 1, 0 ) + y.substring( 1 );
}
else
{
/* Remove the currency sign */
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
x = x.substring( 1 );
y = y.substring( 1 );
}

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

return x - y;
};


/***
* Function to sort datatable's currency columns in descending order.
*/
jQuery.fn.dataTableExt.oSort['currency-desc'] = function(a,b)
{
/*Removing HTML, comma and space from the input data*/
a = String(a).replace(/<[^>]*>?/g, "").replace(/ /g, "").replace(/,/g, "");
b = String(b).replace(/<[^>]*>?/g, "").replace(/ /g, "").replace(/,/g, "");

/* Remove any commas (assumes that if present all strings will have a fixed number of d.p) */
if(a.substring( 1, 0 ) == '-')
{
/* Remove the currency sign, after the minus! */
var x = a == "-" ? 0 : a.replace( new RegExp(/[^-0-9.]/g), "" );
var y = b == "-" ? 0 : b.replace( new RegExp(/[^-0-9.]/g), "" );
x = x.substring( 1, 0 ) + x.substring( 1 );
y = y.substring( 1, 0 ) + y.substring( 1 );
}
else
{
var x = a == "-" ? 0 : a.replace( /,/g, "" );
var y = b == "-" ? 0 : b.replace( /,/g, "" );
x = x.substring( 1 );
y = y.substring( 1 );
}

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

return y - x;
};

[/code]

As I mentioned above, this works fine in Firefox but IE and Chrome are not sorting the column correctly. One thing I have noticed however, if you sort another column first and then sort the column with negative numbers, it works fine the first time. But after you click on it twice, it starts sorting randomly.

I will really appreciate any help on this.

Replies

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    This looks a little funny: x = x.substring( 1, 0 ) + x.substring( 1 ); - doesn't that just give you 'x'? Also why x = x.substring( 1 ); in the other block?

    I'd suggest adding in a few console.logs of a, b, x and y before the final calculation is done and check that they are what you would expect.

    Allan
  • amithaniamithani Posts: 4Questions: 0Answers: 0
    Hi Allan,

    Thank you for taking time to post your comment. I found the issue with the code - it wasn't checking for a negative number for variable "a" and only checking for variable "b". So it wasn't removing the dollar sign if the variable "a" was a negative number and this caused the parseFloat to generate NaN. Anyway, Thanks for your help. I really appreciate it.
  • Anam_UIAnam_UI Posts: 1Questions: 0Answers: 0
    It looks like this is an old post but Amit, did your modified code eventually worked on IE? If it did, could you please tell us what change you made? I tried your code and it works fine on Firefox, but having problem on IE and Chrome.
This discussion has been closed.