Numeric sorting with inches

Numeric sorting with inches

smasonsmason 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

Answers

  • kthorngrenkthorngren Posts: 20,141Questions: 26Answers: 4,736

    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:

         columnDefs: [
           { targets: 3,
            render: function (data, type, full, meta) {
                     if (type == "sort" || type == 'type') {
                       var num = data.split(" ");
                       if (num.length == 1) {
                         return parseFloat(num[0]);
                       } else {
                         var fraction = num[1].split('/');
                         var decimal = fraction[0] / fraction[1];
                         return parseFloat(num[0]) + decimal;
                       }
                     }
                return data;
            }
           }
         ]
    

    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

  • allanallan Posts: 61,438Questions: 1Answers: 10,052 Site admin

    Clever solution Kevin. I do love a little bit of orthogonal data :smile:.

    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

  • smasonsmason Posts: 25Questions: 11Answers: 0

    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.

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

    less than 1 inch then it doesn't work quite right

    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

  • smasonsmason Posts: 25Questions: 11Answers: 0

    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.

This discussion has been closed.