Search code examples
javascriptjquerytablesorter

Sort numerically on string 'xx,xx€' in TableSorter


I am trying to sort the price of my cart items with the tablesorter. Result ascending sort:

9,20€   
8,00€   
7,23€   
6,70€   
5,70€   
12,00€  
11,00€  

My Parser :

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

How can I fix this sort problem?


Solution

  • Solved with :

     ts.addParser({
                id: "euroValue",
                is: function(s) {
                    //u20AC = €
                    return /^\d+,\d+\u20AC$/.test(s);
                },
                    format: function(s) {
                    //replace comma and €-Symbol
                    return jQuery.tablesorter.formatInt( s.replace(/[, \u20AC]/g,'') );
                },
                type: "numeric"
    });