Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting comma delimited large numbers in numeric order #890

Closed
nboughton opened this issue Apr 30, 2015 · 2 comments
Closed

Sorting comma delimited large numbers in numeric order #890

nboughton opened this issue Apr 30, 2015 · 2 comments

Comments

@nboughton
Copy link

I found that the "digit" metadata sorter doesn't work on large numbers that contain commas. My current fix was to add the following code to the jquery.tablesorter.js

    ts.addParser({
        id: "humanReadableNumber",
        is: function (s) {
          return /^[\d,]+$/.test(s);
        }, format: function (s) {
          return s.replace(/,/g, ''); 
        }, type: "numeric"
    });  

Is there a better way to do this?

@Mottie
Copy link
Owner

Mottie commented Apr 30, 2015

Hi @nboughton!

The default digit parser should be replacing commas & decimals based on the usNumberFormat option.

I'm not sure what is meant by "humanReadableNumber", since to me "one hundred and fifty" is a human readable number, and the above parser has a regular expression in the is function detecting numbers. The format function is very generic and would throw a javascript error if the s value was undefined (empty table).

There is already a named numbers parser (parser-named-numbers.js) with a demo. It will need an update to ignore commas, so I'll push a fix for that shortly.

@nboughton
Copy link
Author

Human readable may have been a poor label. Comma delimited is probably more apt.

i.e 1,234,567

What I found using the 'digit' parser was that all numbers after the first comma were ignored in the sort order. So

1,234,567
234
74,234
23
23,546

was sorted to
1,234,567
23
23,546
74,234
234

The problem is that your format function uses parseFloat. Whilst the "is" function will match a comma delimited number parseFloat will not return correctly, see the following:

$ node -e "console.log(parseFloat('1,234,567'));" 
1

$ node -e "console.log(parseFloat('234,567'));"
234

$ node -e "console.log(parseFloat('12,234,567'));"
12

The solution is to modify the digit parser to operate in a similar way to the currency parser

node -e "var n = '12,234,567'; console.log(parseFloat(n.replace(/[,]/g, '')));"
12234567

So the format function line becomes

    return $.tablesorter.formatFloat(s.replace(new RegExp(/[,]/g), "");

I'm not sure if this would cause issues with other number formats but it definitely fixes my problem :)

@nboughton nboughton changed the title Sorting human readable large numbers in numeric order Sorting comma delimited large numbers in numeric order May 1, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants