Question

I have a table with prices in this format: "1.234,56", (the thousands separator is a period, and the decimal separator is a comma). This format doesn't work because tablesorter plugin sees it as strings rather than as a number whenever there's a different character inside (only numbers, +/- and "." for decimals are allowed).

How can I remove the periods and replace the commas with periods before sorting?

Was it helpful?

Solution

Ok, I think I solved it. My table has currency so I edited the 'currency' parser but you could basically do it with any other. Currency parser looks like this in the end:

ts.addParser({
    id: "currency",
    is: function(s) {
        return /^[£$€?.]/.test(s);
    },
    format: function(s) {
        s=s.replace('.', '');
        return $.tablesorter.formatFloat(s.replace(new RegExp(/[^0-9.]/g),""));
    },
    type: "numeric"
});

(by the way, how do you turn on synthax highlight here on stackoverflow?)

OTHER TIPS

'1.234,56'​.replace('.', '').replace(',', '.') // '1234.56'
$.tablesorter.addParser({ 
        // set a unique id 
        id: 'pesos', 
        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(/' '/g,'').replace(/\./g, ''); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    });
    $("#menuh").sticky({topSpacing:0});
    $("#myTable").tablesorter();
    $("#myTableBienes").tablesorter({ 
            headers: { 
                5: { 
                    sorter:'pesos' 
                } 
            } 
        });


});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top