"Natural Sorting" plugin, sorting for "Individual column searching (select inputs)" feature

"Natural Sorting" plugin, sorting for "Individual column searching (select inputs)" feature

kenderkender Posts: 11Questions: 3Answers: 1
  • cannot link to page, closed beta site with IP restrictions

Looking at the Natural Sorting plugin
It works great to sort the data in my form, but I am using that data to create select inputs for searching the fields

I tried replacing this line
column.data().unique().sort().each(function (d, j) {

with
column.data().unique().naturalSort().each(function (d, j) {

hoping it would naturally work, but of course, no such luck,
jquery.min.js?ver=3.6.1:2 Uncaught TypeError: column.data(...).unique(...).naturalSort is not a function

is there a way to get the natural sorting to work with the select inputs?

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Maybe one of the solutions in this SO thread will help sort your data for the select options.

    Kevin

  • kenderkender Posts: 11Questions: 3Answers: 1

    @kthorngren

    The closest I found from that, (actually that lead me to a different way of thinking about it) is this, which sorts whole numbers, but is not sorting decimals

    Thanks for changing my thinking, and it is almost there, but more help would be appreciated if you have it to offer

    $('#product-grid-filters > div').each(function() {
        var dropDownList = $(this).find('select');
        var $options = $(dropDownList).find("option");
    
        var reAlpha = /[^a-zA-Z]/g;
        var reNumeric = /[^0-9]/g;
        $options.sort(function AlphaNumericSort($a,$b) {
          var a = $a.text;
          var b = $b.text;
          var aAlpha = a.replace(reAlpha, "");
          var bAlpha = b.replace(reAlpha, "");
          if(aAlpha === bAlpha) {
            var aNumeric = parseFloat(a.replace(reNumeric, ""), 10);
            var bNumeric = parseFloat(b.replace(reNumeric, ""), 10);
            return aNumeric === bNumeric ? 0 : aNumeric > bNumeric ? 1 : -1;
          } else {
            return aAlpha > bAlpha ? 1 : -1;
          }
        });
      });
    
  • kthorngrenkthorngren Posts: 21,343Questions: 26Answers: 4,954

    Can you put together a simple test case that shows what you are trying to sort? Doing so will allow us to offer suggestions.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • kenderkender Posts: 11Questions: 3Answers: 1

    @kthorngren
    https://live.datatables.net/janewogi/1/edit?html,css,js,output

    if you look at the multiselect box DCR "DC Resistance (DCR)" you will see where it is not sorting naturally

    Sorry if it is taking me a while to respond, I am not getting email notification of replies here

  • allanallan Posts: 63,523Questions: 1Answers: 10,473 Site admin
    Answer ✓

    Unfortunately the sort() method does not sort data using the type plug-in for the column.

    However, you can pass a sort function to the sort() method, just like you can with a regular Javascript array sort. In this case you want to pass it the naturalSort function: https://live.datatables.net/janewogi/2/edit .

    Note I've taken the function out of the IIFE to make it accessible.

    Allan

  • kenderkender Posts: 11Questions: 3Answers: 1

    That did it, thank you very much @allan

    I had tried adding the naturalSort filter to the sort function but it didn't take, my problem was that it was wrapped in the IIFE

Sign In or Register to comment.