The Montoya Herald — ChristianMontoya.com
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.
Couldn't this be solved using parseFloat? Admittedly not for the multilanguage number formatting but in this case it would work.
Nice redesign
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.
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!
Thank you, saved me some time
Thanks for this. Appreciate it.
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.
Thanks, that was really annoying me with table sorter!
Wow, this saved me so much time! Thanks for sharing
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!
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.