Tasoli
Tasoli

Reputation: 29

Tablesorter sort multiple dollar signs

I want to use tablesorter to sort a column of table cells with a number of dollar signs in them. The amount of dollar signs will range from one $ to five $$$$$, and should sort with 1 being the lowest and 5 being the highest. Note: I need these strings to be matched exactly because there will be many dollar signs being thrown around, so $$$ needs to be differentiated from $$, and $.

Here is the code I'm currently using, it does NOT work when I have the dollar signs in, however when I replace them with "b" and "a", it works perfectly. I have 2 dollar signs representing 1 because I read that this is necessary to represent the dollar symbol in a regex. I've also tried \$. Could this be caused by the way the dollar signs are entered in the HTML table? I've used both the standard "$", and "$", neither work correctly.

   // add parser through the tablesorter addParser method 
    $.tablesorter.addParser({ 
        // set a unique id 
        id: 'pricerange', 
        is: function(s) { 
            // return false so this parser is not auto detected 
            return false; 
        }, 
        format: function(s) { 
            // format your data for normalization 
            return s.toLowerCase().replace(/\bd\b/g,3).replace(/\bc\b/g,2).replace(/\b$$$$\b/g,1).replace(/\b$$\b/g,0); 
        }, 
        // set type, either numeric or text 
        type: 'numeric' 
    }); 

    $(function() { 
        $("myTable").tablesorter({ 
            headers: { 
                3: { 
                    sorter:'pricerange' 
                } 
            } 
        }); 
    }); 

Upvotes: 2

Views: 323

Answers (1)

Beetroot-Beetroot
Beetroot-Beetroot

Reputation: 18078

Try

return s.match(/\$/g).length;

Upvotes: 2

Related Questions