I love using jquery.tablesorter to add quick sorting to my reports. It is fast and super easy and doesn’t require a postback or AJAX calls. But every once in a while, I run into a limitation. For example, it seems to be sorting my currency values as if they were text.
For example:
Instead of sorting them like this –
$ 50.00
$ 75.00
$ 300.00
$ 1,000.00
It is sorting them like this –
$ 1,000.00
$ 300.00
$ 50.00
$ 75.00
To resolve this issue, you can add a custom parser like so:
$.tablesorter.addParser({
// set a unique id
  id: ‘currency’,
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(‘$’, ”).replace(/,/g, ”);
  },
  // set type, either numeric or text
  type: ‘decimal’
});
$(“#MyReport”).tablesorter({
  // sort on the first column and third column, order asc
  sortList: [[1, 0], [2, 0]],
  // add custom sort (zero-based index) – this will sort the 7th column
  headers: {
    6: {
      sorter: ‘currency’
    } ,
    7: {
      sorter: ‘currency’
    }
  }
});
<table id=”MyReport” class=”tablesorter”>
. . .
</table>
Advertisements