1

If you check out this JS fiddle: http://jsfiddle.net/DE5Bp and try to sort the Tariff column which I want to sort by the first digit e.g the minutes.

How can I solve this issue?

You can see I have tried to trick the plugin with the <span class="hide"> although this hasn't solved the issue.

UPDATE: http://jsfiddle.net/6jAyF/ this version forces the sorter:"integer"

John Magnolia
  • 16,769
  • 36
  • 159
  • 270

1 Answers1

1

I believe you want to use addParser with type: numeric to get it to work. Here's a code snippet from http://tablesorter.com/docs/example-parsers.html that uses a numeric sorter.

// add parser through the tablesorter addParser method 
$.tablesorter.addParser({ 
    // set a unique id 
    id: 'grades', 
    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(/good/,2).replace(/medium/,1).replace(/bad/,0); 
    }, 
    // set type, either numeric or text 
    type: 'numeric' 
}); 

$(function() { 
    $("table").tablesorter({ 
        headers: { 
            6: { 
                sorter:'grades' 
            } 
        } 
    }); 
});
Waynn Lue
  • 11,344
  • 8
  • 51
  • 76
  • Exactly what I was looking for. Surprised this is not built into the plugin as default. To solve my issue fully I need to get the first number http://stackoverflow.com/questions/609574/get-the-first-ints-in-a-string-with-javascript – John Magnolia Feb 25 '12 at 11:06
  • 1
    @JohnMagnolia You might want to just use `parseInt()` within the custom parser to sort your values properly - [demo](http://jsfiddle.net/Mottie/6jAyF/2/). – Mottie Feb 29 '12 at 20:25