Skip to content

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

@francesco-cattoglio

Description

@francesco-cattoglio

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions