The Montoya Herald, a weblog about Blueprint, jQuery, design, music and life, publishing on the web since September 2005. Written by Christian Montoya: developer, designer and entrepreneur.

The Montoya Herald — ChristianMontoya.com

Search

Buy My DVD!

Like What I Do?

My Amazon.com Wish List

On this domain

Elsewhere

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

Posted on November 14, 2008.

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.

Get a trackback link

10 Comments

  1. Robert O'Rourke on November 17, 2008

    Couldn't this be solved using parseFloat? Admittedly not for the multilanguage number formatting but in this case it would work.

    Nice redesign :)

  2. Christian Montoya on November 17, 2008

    Robert, the challenge was detecting a comma-delimited number, not parsing it. Without this custom parser, the plugin treats them as text strings. I could try parseFloat instead of the string replace, but that's a minor change.

  3. Robert Vrabel on December 18, 2008

    Thanks so much for this snipet of code! I was running into the same exact problem sorting scores on my website. Lets hope this gets rolled into the official release (as it should). Many thanks!

  4. Felix on March 17, 2009

    Thank you, saved me some time :)

  5. Scott on April 28, 2009

    Thanks for this. Appreciate it.

  6. Jan on May 4, 2009

    Hi guys.

    I have some comma numbers that looks like this:

    1,7 km.
    38 km.
    117 km.
    4,3 km.

    For some reason it does not list "117 km." as the highest when I sort it. I've added the parser to the tablesorter.js file - is there more I should do?

    Thanks.

  7. Neal G on May 5, 2009

    Thanks, that was really annoying me with table sorter!

  8. Philip Morton on December 18, 2009

    Wow, this saved me so much time! Thanks for sharing :)

  9. kenneth on March 4, 2010

    Thank you very much for sharing this. Jquery table sorter is one of the best table sorters i have seen. Now that you shared this it's even better now!

  10. Andrew Puig on August 11, 2010

    Thank you Christian for this useful info… You've saved my day!…

    This format is really useful for accounting numbers and large numbers for statistical purposes.

Leave a comment

Use Markdown or basic HTML. For posting code, use Postable. Please keep comments respectful and on topic.