jQuery tablesorter - Not sorting column with formatted currency value

SyAu picture SyAu · Jan 27, 2012 · Viewed 13k times · Source

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.

Answer

Beetroot-Beetroot picture Beetroot-Beetroot · Jan 27, 2012

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.