Search code examples
javascriptjquerytablesorter

jquery tablesorter not sorting negative currency correctly


tablesorter won't sort 1 column correctly but another it does!

I've tried a few different custom parsers but none seem to work. See the example here

http://jsfiddle.net/jybMB/

format: function(s) {
            // format your data for normalization

            var value = replaceAll(',','',s.toLowerCase());

            //value = replaceAll('-','',value);

            //return parseFloat(value.replace('$', ''));

            return $.tablesorter.formatFloat(value.replace(new        RegExp(/[^0-9-.]/g),""));
        },

You can see that Col1 doesn't get sorted correctly and but Col2 does. I suspect it's something to do with the negative symbol but strange that Col2 does work without any custom parsers.


Solution

  • How about something like this:

    Live Demo

    $.tablesorter.addParser({
        // set a unique id
        id: 'money', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            return parseInt(s.replace(/\$/,'').replace(/\,/,''),10); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
    
    
    $("#servicesTable").tablesorter({
        sortList: [[0,1]], 
        headers:
        {
            0 : { sorter: "money"  },
            1 : { sorter: "money"  }           
        }  
    });