How to sort column containing chapter value (1.1, 1.2...,1.10)
How to sort column containing chapter value (1.1, 1.2...,1.10)
Hi,
I'm faced with a "problem" to sort values in a column that contain chapter values (think of it as a book) when I sort/order them with 'order': [[0, 'asc']], I get this order:
-1.1
-1.10
-1.11
-1.12
...
-1.2
But obviously that's not what I want I want:
- 1.1
- 1.2
- 1.3
- ....
- 1.10
- 1.11
- 1.12
- ...
- 1.19
- 1.20
Any hint on how to achieve that ? I was able to find a ugly "hack" by adding an extra hidden column and splitting 1.1 into [1] [0][1] , 1.11 into [1][0][11] and 1.2 becomes [1][0][2] and so on... but that can't be the right way
Answers
Take a look at the various sorting plugins DataTables has; I can't remember which one but one of these does sort numbers correctly like what you're wanting (perhaps the Natural Sorting plugin?): https://datatables.net/plug-ins/sorting/
Thanks for the pointer but I thing it also suffers from the same issues
https://github.com/overset/javascript-natural-sort/issues/13
I'll still give it a shot and will report if it works but it seems unlikely.
Need to laugh at this one, as it is the exact inverse of most questions about sorting and numbers. Normally what happens is DataTables string sorts a column that developers want to be number sorted. You want a number column to be sorted as strings!
The way to do that is to use
columns.type
and set it to bestring
. That will overrule DataTables numeric type detection and string sort your numbers .Allan
Hi Sorry I dropped the ball and felt kinda discouraged Unfortunately it is not working unless I'm missing something.
Here is a sample I tried to put together using your suggestion: https://jsfiddle.net/hd7ns6ag/
Would really appreciate any help to sort this the right one.
Added the expected order to avoid confusion: https://jsfiddle.net/r0b1n1sl4m/vb2og2qc/
Yes you are right, the string doesn't work either. The built in number sort doesn't work the way you want since
1.12
is < 1.2 but > 1.1. It sounds like you want the second part of the number not to be treated as a decimal place but rather as an integer itself.You said you "obviously" wanted that sort order before. Can I ask why "obviously"? Its not in numeric order like that. I'm not actually certain of the right way to do this due to the fact that it isn't in numerical order. Its a bit of a mix of string and number ordering.
Allan
I've just knocked out a new sort plugin that will do this - see here.
It's not pretty, but it does work, and I could tidy this up and release it officially. It's based on the IP Address plugin, as the problem is moreorless the same. As it stands, it'll work with a three-deep chapter number (x.y.z), and each number has to be less than 9999.
Can you take a nose and see if that does the trick. This is it here with @Duked 's original fiddle.
Cheers,
Colin
Allan:
I probably shouldn't have used "obviously" I guess it's really context specific. My usage of dataTables is for book chapters of specification sections so it made sense. I thought it was a pretty common use case but I was apparently wrong.
But Colin's post did the trick !
Colin:
You are my hero ! It works and does a perfect job ! there's a small typo in your fiddle preventing it from working but I forked it so people who encounter the same issue will see it works and won't be discouraged: https://jsfiddle.net/n2wwv1ng/
You should release it as a plugin !
Allan:
I'm think you should totally add that to this plugin to the list of official ones ! It might save people tons of time when trying to sort chapters.
Hi @Duked ,
Excellent, glad to hear it did the trick. Almost 'officialised' it now!
Cheers,
Colin