Sorting problem on numbers when using the from option
Describe the bug
The sorting is wrong on numbers when creating from an html table.
To Reproduce Steps to reproduce the behavior:
- create an html table, with a column id and a incremental number on each row : 1, 2, 3, 4, .... and create a new Grid() from that table
- Click on sort on that column
- you will have it sorted like a string : 1, 10, 100, 101, 102 103, ...
Expected behavior This should sort it like a number 1, 2, 3, 4 becomes 4, 3, 2, 1
Screenshots
Desktop / Smartphone not os specific
Running into the same issue. I searched the coding of Gridjs and looked for a way to set a custom sorter for a from-table generated table like documented in https://gridjs.io/docs/config/sort#column-specific-sort-config, but did not found a solution for this. Is there a way to configure column specific sorting when using from?
Would like an answer on this one if possible 👍
@barberob @matzeeable it sounds like you are trying to perform a numeric comparison without using a custom comparator function. In JavaScript array.sort() converts the elements of the array to strings and then compares their sequences of UTF-16 code unit values (lexicographical, not numerical). In order to achieve the desired result, you must use a custom comparator function to achieve numeric sorting.
@afshinm one thing you could probably change is to first check if the values being sorted are numeric, but that will likely come with its own challenges, especially considering there is already a way to achieve numeric sorting with a custom comparator function.
Hi, so what about, if gridjs concatenate column data, check, if there are only numbers and not use array.sort()?
@kopepasah I understand the problem here, but i can't use custom sort when i'm using from table, am i right ?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.