Numeric values in DataTable aren't sorted numerically

Numeric values in DataTable aren't sorted numerically

StupaStupa Posts: 4Questions: 1Answers: 0

Hello !

I am using DataTables version 1.10.19 and I am facing a quite strange issue.

I have a column with numeric value. So I added "num" for the type of the column, but when I'm trying to sort values on the column, Values are sorted as strings, so like "10" / "120" / "60". I would like values to be sorted as "10" / "60" / "120".

Am I missing something ? Here's the class of my column :

export default function donneeDynamique(entete)
{
    return {
        'data': $(entete).data('entete-name'),
        'searchable': false,
        'type': 'num',
        'render': function (data) {
            if (data == null) {
                return ''
            } else {
                if (data.type === 'checkbox') {
                    return createCheckbox(data.valeur)
                } else if (data.type === 'fichier') {
                    return createLinkFile(data.valeur)
                } else if (data.type === 'date' || data.type === 'dateheure') {
                    return formatDateTime(data.valeur, data.type);
                } else {
                    return data.valeur;
                }
            }
        }
    }
}

Just ignore the cases with data.type equals to "checkbox", "fichier", "date" or "dateheure". In my case render() will always return data.valeur

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770
    edited June 2021

    The Datatables type detection will override what you set for the columns.type. It scans ll the data in the column to make sure they are all the same type. If not it will set the type to string. This suggests there are non-numeric values in that column. Without seeing the data you have it will be difficult to diagnose. Please post a link to your page or a test case replicating the issue so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • StupaStupa Posts: 4Questions: 1Answers: 0
    edited June 2021

    I can't really provide a link. But I can show you, all the values are numeric

    The column I am trying to sort numerically is the "a" one.

    I will try to recreate the bug on an public project on Monday if this helps :smile:

  • kthorngrenkthorngren Posts: 20,309Questions: 26Answers: 4,770

    Please do create a test case that we can look at. You can look at the examples, different data sources, to see that the Age column is sorted as a number not string:
    https://datatables.net/examples/data_sources/index.html

    We will need to debug your specific data and configuration.

    Kevin

  • StupaStupa Posts: 4Questions: 1Answers: 0

    Hello !

    I tried to recreate a test case but it was working just fine with what I did. So I'm guessing the problem might be the complex configuration of our datatable.

    I used the datatable deug tool to upload my configuration. So it gave me this link, no idea if this might help because I'm unable to see what's in the link since I am not a SpryMedia employee.

    https://debug.datatables.net/ibefag

    I don't know if you could have a look and see something which would explain why the values aren't sorted numerically :smiley:

  • allanallan Posts: 61,726Questions: 1Answers: 10,109 Site admin
    Answer ✓

    Yes, very helpful - thank you.

    From it I can see that you have "serverSide": true, - which means that all ordering is done by the server-side. In case your /fr/patients/list/6 endpoint.

    Do you have that configured to handle a DataTables server-side request?

    The debugger says you only have 17 rows in the table. Normally server-side processing wouldn't be needed until you have tens of thousands of rows at least.

    Allan

  • StupaStupa Posts: 4Questions: 1Answers: 0

    Thanks a lot !! This was the problem. I thought ordering wasn't handled by the server but this is indeed the case. I managed to find a way to change my request and fix the ordering !

    Thank you once again you really helped me on this one ! :smiley:

Sign In or Register to comment.