Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting strings with letters and numbers #1571

Closed
ecz4 opened this issue Jul 27, 2018 · 3 comments
Closed

Sorting strings with letters and numbers #1571

ecz4 opened this issue Jul 27, 2018 · 3 comments

Comments

@ecz4
Copy link

ecz4 commented Jul 27, 2018

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

@Mottie
Copy link
Owner

Mottie commented Jul 27, 2018

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
Copy link
Author

ecz4 commented Jul 30, 2018

This is awesome! Thanks a lot!

@Mottie
Copy link
Owner

Mottie commented Aug 27, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants