S16
S16

Reputation: 2995

jQuery table sorting and currency issue

This one may be a bit specialized, but here goes anyway:

Page for reference: http://greg-j.com/icvm/anticartel/search-results.html

Plugin for reference: http://tablesorter.com/

If you look at the last two columns for "Total Fines", you'll see the currency output includes $x.xxx billions and $x.xxx millions. The built-in currency parser does not account for this format. The plugin, fortunately, allows for you to write your own parser. However, I'm not getting anywhere.

Upvotes: 2

Views: 540

Answers (2)

Steve Lewis
Steve Lewis

Reputation: 1302

See if this works, I haven't tested it:

$.tablesorter.addParser({ 
    id: 'monetary', 

    'is': function(s) { 
        return false; 
    }, 

    format: function(s) { 

        var i = s.split('$').join('');

        var suffixes = [
          {name:'thousand', mult: 1000},
          {name:'million', mult: 1000000},
          {name:'billion', mult: 1000000000},
          {name:'trillion', mult: 1000000000000}
        ]; 

        for (var j in suffixes) { 
          if (i.indexOf(' '+suffixes[j].name) != -1) {

              i = i.split(' '+suffixes[j].name).join('');

              val = parseFloat(i) * suffixes[j].mult;
          } 
        }

        return val; 
    },  

    type: 'numeric' 
}); 


$("#cartels").tablesorter({
    widgets: ['zebra'],
    sortList: [[0,0]], 
    headers: { 
        4: { 
            sorter:'monetary' 
        },
        5: { 
            sorter:'monetary' 
        }
    } 
});

Upvotes: 3

The Muffin Man
The Muffin Man

Reputation: 20004

Can you post the code you've tried?

It looks like what they are doing in the example you posted is assigning each word with a number representation, and then sorting by that:

return s.toLowerCase().replace(/good/,2).replace(/medium/,1).replace(/bad/,0);

So in your case one way might be to replace million with the correct number of zeros and the same for billion. So essentially $1 million gets evaluated to $1,000,000 as far as the parser is concerned.

return s.toLowerCase().replace(/million/,000000).replace(/billion/,000000000);

So s is evaluating to $1000000 once the replace function is evaluated. Just a thought. Not sure if it works, but it might get you on the right track.

Upvotes: 1

Related Questions