Sorting - doesn't always work
Sorting - doesn't always work
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?
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?
This discussion has been closed.
Replies
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
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
Will the sorting feature break if I have column headers that span 2 columns?
What you can do is something like this: http://datatables.net/examples/advanced_init/complex_header.html
Allan