Mottie/tablesorter

Sorting strings with letters and numbers

Closed this issue · 3 comments

ecz4 commented

Hey,

I think I found a problem when sorting columns containing number and letters, just like this example's first column: https://mottie.github.io/tablesorter/docs/example-options-headers-digits-strings.html

ASC sort puts A1 in front of A02. That's not the expected result, as 0 < 1. I'm under the impression leading zeros are ignored and in this case it sorts as the values were A1 and A2. My data contains lots of leading zeros mixed with text and getting rid of them is not an option I have.

Is it a bug or a feature? Is there a way to change this behaviour through configuration?

Another related question: Is there a doc page listing all the sorting options (css classes)? I could only find sorter-text and sorter-digit.

Thanks,
Ed

Hi @ecz4!

The internal natural sort algorithm uses chunking to sort a mixture of text and numbers, it does ignore leading zeros. So, if you want to use the built-in JavaScript sorting method, which should behave as you need, then set the textSorter option for that column to use a basic sort method as follows (demo):

$(function() {
  $("table").tablesorter({
    theme: "blue",
    textSorter: {
      0: function(a, b) {
        return a > b ? 1 : a < b ? -1 : 0;
      }
    }
  });
});

On the doc page, you can look under the headers option, which has will redirect you to the config.parsers entry... it should be more obvious, so I'll add a new "built-in parsers" demo with a full list.

ecz4 commented

This is awesome! Thanks a lot!

I'm guessing this issue has been resolved, so I'm going to close it. If you continue to have problems, please feel free to continue the discussion in this thread.