jQuery 1.7.1 & tablesorter plugin - I have a currency column with thousand separators and values like $52.00 $26.70 $100.00 $50.00 $1,002.00 $1,102.00. When I try to sort getting sorted in the following way,
$1,002.00 $1,102.00 $26.70 $50.00 $52.00 $100.00
Need values like,
$26.70 $50.00 $52.00 $100.00 $1,002.00 $1,102.00
Tried many solutions mentioned here, but no success.
Tablesorter allows you to define "custom parsers" for things like this.
// add parser through the tablesorter addParser method $.tablesorter.addParser({ // set a unique id id: 'thousands', is: function(s) { // return false so this parser is not auto detected return false; }, format: function(s) { // format your data for normalization return s.replace('$','').replace(/,/g,''); }, // set type, either numeric or text type: 'numeric' }); $(function() { $("table").tablesorter({ headers: { 6: {//zero-based column index sorter:'thousands' } } }); });
You may have to tweak the format function, which I've not tested.
If you just want to fix currency numbers (quickest):
<script type="text/javascript"> $("table").tablesorter({ textExtraction: function(node){ // for numbers formattted like €1.000,50 e.g. Italian // return $(node).text().replace(/[.$£€]/g,'').replace(/,/g,'.'); // for numbers formattted like $1,000.50 e.g. English return $(node).text().replace(/[,$£€]/g,''); } }) </script> <td><span>£80,000.00</span></td>
I don't like these 3 other proposed solutions on StackOverflow:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With