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

Comma seperated numbers #123

Open
shanejones opened this issue Dec 11, 2016 · 5 comments
Open

Comma seperated numbers #123

shanejones opened this issue Dec 11, 2016 · 5 comments

Comments

@shanejones
Copy link

I have an issue where comma separated numbers aren't sorting the same as non comma separated numbers.

Doing a Low to High sort results in the following

  • 0
  • 1,223
  • 10,000
  • 12
  • 123
@juanalvarezg
Copy link

There is a workaround for this

    $(function () {
        $.tablesorter.defaults.groupSeparator = ",";
        $.tablesorter.formatFloat = function (s) {
            var regexp = new RegExp("[" + this.defaults.groupSeparator + "]","g")
            var i = parseFloat(s.replace(regexp, ''));
            return (isNaN(i)) ? 0 : i;
        };
        $("#myTable").tablesorter();
    });

@nekromoff
Copy link

does not work for me

@Mottie
Copy link
Collaborator

Mottie commented Jul 17, 2017

You'd need to add a custom parser (demo):

var unitSeparator = ',';

var regex = new RegExp('[\s' + unitSeparator + ']', 'g');

$.tablesorter.addParser({
  id: 'removeDigitSep',
  is: function() {
    // don't autodetect
    return false;
  },
  format: function(s) {
    var num = $.tablesorter.formatFloat(s.replace(regex, ''));
    // return numeric value or original string
    return $.isNumeric(num) ? num : s;
  }
});

$('table').tablesorter({
  headers: {
    0: {
      sorter: 'removeDigitSep'
    }
  }
});

@rafic20
Copy link

rafic20 commented Dec 20, 2017

Replying to @juanalvarezg
This worked in terms of sorting correctly, but now I can only sort once. On my first click, it sorts in descending order. On my second click, the table "flashes" very briefly but the display not change.

There is a workaround for this

$(function () {
        $.tablesorter.defaults.groupSeparator = ",";
        $.tablesorter.formatFloat = function (s) {
            var regexp = new RegExp("[" + this.defaults.groupSeparator + "]","g")
            var i = parseFloat(s.replace(regexp, ''));
            return (isNaN(i)) ? 0 : i;
        };
        $("#myTable").tablesorter();
    });

@juanalvarezg
Copy link

I dont know why :( this workaround just makes sure that we parse a float number. the sorting happens outside this logic. I did that on january 2017. Are you using the latest component? if so, maybe try with the version that was released around january 2017.
good luck!

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

No branches or pull requests

5 participants