Need Help with "natural.js" sorting

Need Help with "natural.js" sorting

mark789mark789 Posts: 11Questions: 3Answers: 0

Link to test case: https://jsfiddle.net/pmi2018/7etqo15k/21/
Debugger code (debug.datatables.net):
Error messages shown:
Description of problem: The "natural" sorting breaks on a data value of "1,310.5 ft". In the jsfdiddle, be sure to select the 'Show 25 entries' in the top left corner and then sort the Altitude - ft column to see the problem. The Weight and Altitude - m column work as expected, taking into account the "not recorded" entries. But neither of these two columns have a US thousands separator.

I think the regex in the plugin does not take into account thousands separator for US numbers. It does take into account the decimal point separator for US numbers. I am not enough of a regex guy to make the change, so if anyone can look at the code (https://datatables.net/plug-ins/sorting/natural) and tell me where to put the comma in the regex, that would be great! Or, if there is another sorting plugin that will work for my use case, that would be helpful, too. I have tried numString and any-number, and they break on the "not recorded" entries.

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,374Questions: 26Answers: 4,957

    Maybe the numString plugin will do what you want. Looks like it removes all non-digit characters.

    Kevin

  • mark789mark789 Posts: 11Questions: 3Answers: 0

    numString requires a user supplied regex. I created this regex, "[0-9.,]+[ ftgm]+.$", and it handles all the numerical entries but not the "not reported". I also tried to modify numString.js to handle the "not reported" exception, but I am neither a js nor regex programmer, so I have not succeeded. Thanks for your reply.

  • kthorngrenkthorngren Posts: 21,374Questions: 26Answers: 4,957
    Answer ✓

    Maybe the simplest option is to use Orthogonal data and convert the cell data to numeric data for the sort operation. I think you will have problems with "[0-9.,]+[ ftgm]+.$" as this will keep the ft, etc which makes the column a string column. I think you want to sort the column as numeric for sorting. To do this you will need to do the same conversion for type detection. Here is an updated example:
    https://jsfiddle.net/u1ycesqw/

    The example converts cells that don't have numbers and makes the value 0. This way the not reported sorts to the top or bottom. Not sure if this is what you are looking for. If not then we will need to revisit the regex expression for one of the plugins.

    Kevin

  • mark789mark789 Posts: 11Questions: 3Answers: 0

    Kevin,

    That looks great! I need some time to understand it, but it works! numString uses the regex I mentioned to get matches to extract the number for sorting. It just did not handle the non matches very well.

Sign In or Register to comment.