Numeric sorting with inches
Numeric sorting with inches
smason
Posts: 25Questions: 11Answers: 0
Hello,
How can I make datatables sort columns that have inches? For example:
1
1 1/2
2
2 1/4
3
3 1/2
3 7/8
Is this possible? Data tables always tries to sort by string order. I've tried sType: num-html, natural, numeric but they did not seem to help. live example: http://www.sealanddesign.com/page/full-face-gaskets
Thank you for any advice.
This question has an accepted answers - jump to answer
This discussion has been closed.
Answers
Interesting problem. Don't know of a plugin to help. One option is to use
columns.render
to render the fraction into a decimal number for sorting purposes and still display the fraction. Here is what I came up with as a starter:There is no error checking of the value and likely there is a better way to parse the fraction.
You can try it here:
http://live.datatables.net/fonezuyu/1/edit
Kevin
Clever solution Kevin. I do love a little bit of orthogonal data .
The other option is to use a custom sorting plug-in. I think this will be quite an interesting one and will take a look into it.
Allan
kthorngren's answer got me almost there! It works if there is a whole number part, but if it's less than 1 inch then it doesn't work quite right.
Any idea how we can get it to work for items less than one inch too? If there is something that is 3/4 that's smaller than 1 but it would come after.
Made a quick update to the code for this. Again there is no error or format checking. Might get you by until Allan or someone wants to write a custom sorting plugin.
Here is the update:
http://live.datatables.net/fonezuyu/3/edit
Kevin
That worked exactly as needed. It sorts correctly now!
Error checking is not necessary for my purpose because the data is static and unchanging and not user generated at all. That might be an exercise for the future for someone ambitious.