Sorting and column types

Sorting and column types

backseatbackseat Posts: 7Questions: 3Answers: 0

Summary: I have a string-format column that has a defined sort field that is an integer. When I sort that column, it sorts by string, not int.

columns: [
        {data: 'client'},
        {data: { '_': 'support_time',
                 'sort': 'support_time_sort' }
          },
         ...

support_time is an hh:mm:ss string (eg, 2:27:53) and support_time_sort is an integer (actually the number of seconds of time, so in this example 8873). For testing, I'm showing both the support_time and support_time_sort columns. When I sort on the support_time column, it sorts by support_time_sort as a string (ie, 527 before 61); the support_time_sort column correctly sorts as an integer.

I think I need to specify that the support_time column is of num format, but I'm struggling to get the syntax right.

  1. Is that the right way of fixing this?
  2. Could someone give an example of how I specify {type: num} for that column?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736
    Answer ✓

    The developers can confirm but it seems that when rendering the filter value Datatables always treats it as a string not a number. I even tried forcing it with using columns.render and using various techniques like Number() to return a numeric filter value.

    The answer I found is to use orderData. You will need to create a support_time_sort column but can use columns.visible to hide it.

    Here is an example using your config, columns.render and orderData:
    http://live.datatables.net/rajacoge/1/edit

    If you don't want to use orderData then another option might be to use columns.render and return zero padded strings, ie, 0527, 0061, etc.

    Kevin

  • backseatbackseat Posts: 7Questions: 3Answers: 0

    That worked perfectly. Thank you so much for your help - I'd spent ages trying to fix that!

This discussion has been closed.