Override the numeric sorting function in 1.10

Override the numeric sorting function in 1.10

kristofferkristoffer Posts: 4Questions: 1Answers: 0

I'm trying to convert an old application to use the new API. The data has a column that contains integer numbers and empty strings and I want the empty strings to be placed after the numbers when sorted ascending. The built in numeric sorting function will place the empty cells before the numbers.
In my old application, this was solved by overriding the built in numeric sorting algorithm but this seems difficult to do with the new algorithm with the built in handling of formatted number.

Does anyone have a solution to my problem?

Thanks in advance
// Kristoffer

This question has an accepted answers - jump to answer

Answers

  • rhinorhino Posts: 80Questions: 2Answers: 17
    edited July 2014 Answer ✓

    One thing you can do is set the columns.render option of the column.

    When the type of the render is 'sort', return a value larger than you expect to exist in your table:

    $('#myTable').DataTable( {
        columnDefs: [
            {
                render: function( data, type, row, meta ) {
                    if ( type == "sort" && data == "" )
                        return 999999999;
                    return data;
                }
            }
        ]
    });
    
  • rhinorhino Posts: 80Questions: 2Answers: 17

    Here's a working demo

    Though I just realized... if you want this to happen whether it's ascending or descending... well then I'm not sure...

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    If you don't care about the formatting aspect, then the method using in 1.9 or just overwriting the built in sort functions should still work.

    If you do care about the formatting aspect, your sorting functions will need to deformat the data.

    Allan

  • kristofferkristoffer Posts: 4Questions: 1Answers: 0

    Rhinos solution works for me. I changed the return value from 999999999 to Number.MAX_VALUE in my application to make it a little more flexible.

    I tried to use my "old" method of overwriting the default sorting function but didn't get that to work. I will maybe do some more testing to see if I can figure out the problem.

  • allanallan Posts: 61,667Questions: 1Answers: 10,096 Site admin

    i'll try it myself and see if I can figure out what is going on!

    Allan

This discussion has been closed.