Christian Montoya

Extending jQuery's tablesorter plugin to support comma-delimited numbers

I was using the jQuery tablesort plugin on a recent project and I found that it was treating comma-delimited numbers as plain strings. An example of a comma-delimited number is: 99,001. This was a deal-breaker for me, so I looked at the documentation and I came up with the following custom parser. You can add this to the Javascript on your webpage and it will recognize comma-delimited numbers the same way it recognizes plain numbers:

jQuery.tablesorter.addParser({
  id: "fancyNumber",
  is: function(s) {
    return /^[0-9]?[0-9,\.]*$/.test(s);
  },
  format: function(s) {
    return jQuery.tablesorter.formatFloat( s.replace(/,/g,'') );
  },
  type: "numeric"
});

I've also published this at my lab: A Custom Parser for Comma-delimited Numbers with jQuery Tablesort. Hopefully this functionality will be rolled into the main plugin, since that would probably make it faster. Also, I think a bit more work could be done to make it friendly with international-style numbers (99.000,00 is 99,000.00 in other countries). Unfortunately, I only had enough time to come up with this parser, so I leave that up to you, dear reader, to solve.

Thank you for reading • Published on November 14th, 2008 • Please take a moment to share this with your friends

Previously:

More recently: