JQuery Tablesorter - custom parser not working
I am trying to write a custom parser for the jQuery plugin, Tablesorter . The idea is to sort the numbers in descending order on the first click.
However, when I sort the table, the order does not change. Sometimes several lines move, but most of them remain the same. Here is the code:
$.tablesorter.addParser({
id: 'desc',
is: function(s) { return false },
format: function(s) {
return 1.0 / parseFloat( s.replace(/,/g,'') );
},
type: 'numeric'
});
Other parsers I've written work fine. I tried 9999 minus the number instead of 1.0 divided by it, in case this is a floats issue (no luck).
source to share
I found a solution. I had blank cells in each column that parsed as "NaN". Why it screwed up the order, I do not know (empty cells were periodically interrupted with regular numbers, nothing was ordered).
In short, this code works for the format function:
format: function(s) {
if ( s == '' )
return 0;
return -parseInt( s.replace(/,/g,'') );
}
source to share
To sort something in reverse numerical order, the natural way for me is to multiply it by -1, not by the methods you've tried.
As far as the parser itself is concerned, the only difference I notice is that you are returning an actual number, whereas the Tablesorter parsing example site returns a string. Perhaps converting the value back to a string before returning it will work?
source to share