Sorting - doesn't always work

Sorting - doesn't always work

romsokromsok Posts: 38Questions: 0Answers: 0
edited December 2009 in General
I noticed that sorting doesn't work consistently in my tables:

I have the 's in - some of them cover 2 columns in , one for the number, another for percentage.
And when I sort on those columns, some of them sort like this:

This is ascending order

Users signed up
--------------------
346 | 79.72%
365 | 85.08%
553 | 95.84%
0 | 0.00%

and this is descending order

Users signed up
--------------------
0 | 0.00%
553 | 95.84%
365 | 85.08%
346 | 79.72%

In other words - zero somehow always turns out largest. Why?

Replies

  • romsokromsok Posts: 38Questions: 0Answers: 0
    The Tables seem to have a problem with the percentage sign. 0.00% ends up being the largest value, and 100.00% the smallest.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Hi romsok,

    The reason that this happens is that DataTables is seeing the percentage sign as a non-numeric character, and therefore parsing that column as a string, rather than as a number (since it's not only numeric characters).

    The way to deal with this is to use the 'formatted numbers' plug-in: http://datatables.net/plug-ins/sorting#formatted_numbers - this will remove any non-numeric data and then sort on what is left as a number. Set the sType in the column to "formatted-num".

    Regards,
    Allan
  • romsokromsok Posts: 38Questions: 0Answers: 0
    So to make use of this plug-in all I have to do is set the sType to "formatted-num". Sorry, I know this is a stupid question but I am not clear if any additional configuration is required.
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    That's about it yes. You need to include the plug-in code as well of course :-)

    Have a look of the source for my download page ( http://datatables.net/download ) where I've made use of the 'title-numeric' plug-in, if you want an example.

    Allan
  • romsokromsok Posts: 38Questions: 0Answers: 0
    As an extension to this question:
    Will the sorting feature break if I have column headers that span 2 columns?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Short answer is yes: You _MUST_ have a unique TH for each column. If you have one which spans two columns, which column would DataTables sort on...?

    What you can do is something like this: http://datatables.net/examples/advanced_init/complex_header.html

    Allan
This discussion has been closed.