Sorting broken with big numbers

Sorting broken with big numbers

ffeffe Posts: 28Questions: 4Answers: 0

Link to test case:
http://live.datatables.net/hogexoje/1/
Debugger code (debug.datatables.net):
N/A
Error messages shown:
None
Description of problem:
With big numbers, sorting does not work at all or does even sort wrong.

If numbers are too big, sorting should at least fall back to alphabetical search in my opinion.

Answers

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    The numbers in the last two columns are larger than the MAX_SAFE_INTEGER of 9007199254740991 which is why the sorting isn't working as expected. You can use type and set those columns to string if you wish, for example:
    http://live.datatables.net/hogexoje/2/edit

    However if the number if digits isn't the same for each cell in the column sorting still won't be correct. For example:
    http://live.datatables.net/tucutehi/1/edit

    You could create a sorting plugin to handle the columns with large numbers. You can look through these sorting plugins to see if there are any that meet your need or help you develop one. You can use your test case to develop the plugin and if you have any questions we will have something to look at.

    Kevin

  • ffeffe Posts: 28Questions: 4Answers: 0

    Hi Kevin,

    thanks for your answer. I had expected that the numbers in the last two columns have exceeded the MAX_SAFE_INTEGER value, although I did not check it.

    Sure, I can set the type to string for example, but I want to focus on the standard behaviour.

    The problem that I see here are three things:
    1. The forth column is sorting incorrectly which should not happen at all in my opinion
    2. The last columns does not do any sorting. But I am sure this sorting does not happen because the values have been recognized as numbers, which is a bit of contradiction for me
    3. Forth and last column both have only values bigger than MAX_SAFE_INTEGER, but the sorting behaves differently

    Is this somehow expected?

    What about using BigInt? Of course also BigInt is limited, but would significantly increase the number space. Not sure about other implications such as speed, etc.

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974
    edited April 2021

    The forth column is sorting incorrectly which should not happen at all in my opinion

    Take a look at this example:
    http://live.datatables.net/suhopidu/1/edit

    Javascript is converting 111111111111111111 and 111111111111111112 to 111111111111111100 and the other three numbers to 111111111111111120. I don't know how Datatables is sorting these but I added a sort function that shows the results of sorting with the converted numbers. You are seeing similar results in column 4.

    The last columns does not do any sorting. But I am sure this sorting does not happen because the values have been recognized as numbers, which is a bit of contradiction for me

    The last two lines, of the example, show that all the numbers in the last column are converted to the same 1111111111111111200. So it appears sorting is not happening because all the values are the same. Sorting is taking place its just the order is not changed.

    The columns.type docs detail the built-in types that are automatically detected. This example shows the type Datatables auto-assigned to each column:
    http://live.datatables.net/yoqolupu/1/edit

    Column 0 Type string
    Column 1 Type string
    Column 2 Type num
    Column 3 Type num
    Column 4 Type num
    

    What about using BigInt?

    It is possible for you to create a type detection plugin to match the data in your column. Just for fun I created the bigint type detection and the sorting works:
    http://live.datatables.net/keqasaxi/1/edit

    I'll let you test the bigint type detector for accuracy. Just something I quickly built to show you how it can be done.

    Kevin

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    My type detection doesn't allow for a mix of integer and bigints in the same column. It will fall through to the normal Datatables type detection. So you might want to take that check out and create all integers as BigInt. not sure how the BigInt() method works with other number types. Will leave that to you to test :smile:

    Kevin

  • kthorngrenkthorngren Posts: 21,443Questions: 26Answers: 4,974

    I thought about this more and creating the bigint type detector is not enough. This example shows the values in the column data is a string:
    http://live.datatables.net/keqasaxi/2/edit

    An ordering plugin will need to be created to preformat the string to BigInt(). Added this to the example.

    Kevin

This discussion has been closed.