Skip to content

Table sorting does not work with numbers containing thousands separator. #802

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

Closed
francesco-cattoglio opened this issue Feb 7, 2025 · 3 comments
Labels
enhancement New feature or request

Comments

@francesco-cattoglio
Copy link
Contributor

What are you building with SQLPage ?

A reporting tool with big numbers in a table!

What is your problem ? A description of the problem, not the solution you are proposing.

To make the numbers easier to read, I added some formatting with thousands separators, but this breaks column sorting because the sorting internally uses parseFloat(), which stops parsing at the first comma it encounters

Describe the solution you'd like

The easiest solution would be to strip commas from the string before attempting to parse it as a float. At line 36 of sqlpage.js we could replace

        return { num: Number.parseFloat(sort_key), str: sort_key };

with

        return { num: Number.parseFloat(sort_key.replace(/\,/g, "")), str: sort_key };

There is a minor drawback, because strings that contain numbers separated by commas (e.g: 15,16,17) will now be parsed and sorted in a different way. But I do not think that anyone is currently sorting strings like those.

Describe alternatives you've considered

I have not considered other alternatives for now. One might argue that my solution is bad since it is locale-dependent, and that is 100% true. But I think it would still be better than current behavior.
A possible flexible solution would be using some kind of markdown, so that the number used for sorting is different from the string used to display it.

@francesco-cattoglio francesco-cattoglio added the enhancement New feature or request label Feb 7, 2025
@lovasoa
Copy link
Collaborator

lovasoa commented Feb 8, 2025

Hi!
One much bigger drawback is that in many languages , is a decimal separator, not a thousands separator. In Europe, Latin America, and large parts of Africa, 1,000 < 100.

The right solution, I believe, would be to let SQLPage do the number formatting on the client side, using native browser APIs. Would you be interested in opening a pull request implementing that?

@francesco-cattoglio
Copy link
Contributor Author

francesco-cattoglio commented Feb 8, 2025 via email

@lovasoa
Copy link
Collaborator

lovasoa commented Feb 11, 2025

fixed by #809

thank you !

@lovasoa lovasoa closed this as completed Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants