Null Values + Currency Sort

Null Values + Currency Sort

WTHeelWTHeel Posts: 2Questions: 0Answers: 0
edited May 2012 in General
I'm using the great currency plugin to sort, but it breaks when there are blank/null cells. I've tried to integrate one of the blank/null quasi-plugins, but it still does not work. Would appreciate any help!

Here is what I'm starting with and what is live on the site now: http://www.lawschooltransparency.com/clearinghouse/?show=compare&sub=costs

[code] jQuery.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;
};

jQuery.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;
};[/code]

Sort works everywhere but the three columns beginning with "Resident" as those are the columns with blanks.

So then I tried to integrate this:

[code]jQuery.fn.dataTableExt.oSort['mystring-asc'] = function(x,y) {
var retVal;
x = $.trim(x);
y = $.trim(y);

if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= 1;
else if (y == "" || y == " ") retVal= -1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1

return retVal;
}
jQuery.fn.dataTableExt.oSort['mystring-desc'] = function(y,x) {
var retVal;
x = $.trim(x);
y = $.trim(y);

if (x==y) retVal= 0;
else if (x == "" || x == " ") retVal= -1;
else if (y == "" || y == " ") retVal= 1;
else if (x > y) retVal= 1;
else retVal = -1; // <- this was missing in version 1

return retVal;
}[/code]

I added everything from "if(x==y)" to "return retVal;" and defined the retVal variable at the beginning of the function.

At that point, everything went haywire and I had to take it down. The problem is that I don't understand how oSort works, so I don't really know what I should be trying to return in certain scenarios.

Thanks in advance for any help.

Replies

  • allanallan Posts: 63,542Questions: 1Answers: 10,476 Site admin
    Looks like your code can never return 0, which I would suggest is wrong. Why not just modify the original plug-in a little bit like this:

    [code]
    var x = (a==="-" || a==="") ? 0 : a.replace( /[^\d\-\.]/g, "" );
    [/code]

    You could add your trim in as well at the start of the function.

    Allan
  • WTHeelWTHeel Posts: 2Questions: 0Answers: 0
    edited May 2012
    Wonderful, thanks for your help Allan!
This discussion has been closed.