sorting huge number

sorting huge number

BlidaAlgBlidaAlg Posts: 9Questions: 4Answers: 0

i have problem in datatable sorting with huge number like this :
362516690048775976
362516690048775977
362516690048775978
362516690048775979
362516690048775980
362516690048775981

the sorting with huge number not working
thanks

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    edited July 2020

    That number is larger than the Javascript MAX_SAFE_INTEGER value. They are likely being treated as strings which might not sort as expected. You may need to use orthogonal data and prefix leading 0 so all of the strings are the same length for the sort operation.

    If you still need help please build a simple test case with an example of your data and the sorting problem.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • BlidaAlgBlidaAlg Posts: 9Questions: 4Answers: 0

    so the idea it's to change the column from string to integer ??

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923

    so the idea it's to change the column from string to integer ??

    No. I am assuming you have different length numbers. If that is the case then the idea is to use columns.render to prefix the numbers with leading zeros so all strings are the same length. For example the sort operation data should look like this:

    362516690048775976
    362516690048775977
    362516690048775978
    362516690048775979
    362516690048775980
    362516690048775981
    000516690048775981
    000000000000775980
    

    Kevin

  • BlidaAlgBlidaAlg Posts: 9Questions: 4Answers: 0

    I found this solution and it works but I don't know if it is a good solution or not

    $('.datatable').dataTable( {
    columnDefs: [
    { type: 'natural', targets: 3 }
    ]
    } );

  • kthorngrenkthorngren Posts: 21,172Questions: 26Answers: 4,923
    Answer ✓

    You found the Natural Sorting Plugin. Thats a good find.

    Kevin

  • BlidaAlgBlidaAlg Posts: 9Questions: 4Answers: 0

    thanks for you're help and have a good day ^^

This discussion has been closed.