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

Percent sign breaks text sorting #67

Closed
MelTraX opened this issue May 9, 2012 · 3 comments
Closed

Percent sign breaks text sorting #67

MelTraX opened this issue May 9, 2012 · 3 comments
Labels

Comments

@MelTraX
Copy link

MelTraX commented May 9, 2012

Example: http://jsfiddle.net/CuHbQ/1/

And yes, this is a real life example apart from changed names. :D

I changed my parser to this:

    ts.addParser({
        id: "percent",
        is: function(s) {
            return (/[0-9,.]+ *[$€%]\)?$/).test($.trim(s));
        },
        format: function(s) {
            return $.tablesorter.formatFloat(s.replace(/[^0-9.,]/g, "").replace(/,/g, "."));
        },
        type: "numeric"
    });

Kinda combining currency and percent but that's probably not how you want to do it. :D

I need auto-detection of German and English numbers and luckily I don't have delimiters for thousands. I'll probably even combine percent, currency and digit.

@Mottie
Copy link
Owner

Mottie commented May 9, 2012

Hi MelTraX!

Combining currency and percent is fine, in fact the dealing with digits demo has a total column which should be set to currency, but is instead set to digit because the first cell in the column doesn't have a currency symbol, but the column still sorts properly.

The format function does need some changes:

  • Using s.replace(/[^0-9.,]/g,'') as this will replace alphabetic characters with an empty string and thus break the string functionality which allows sorting strings in numerical columns. So that can be changed to match the currency format which also needs to not replace parenthesis or a dash (negative numbers) s.replace(/[^\w,. \-()]/g, "")
  • Using .replace(/,/g, ".") will break some formats. It'd be best to just remove that since the formatFloat function checks the value of the table.config.usNumberFormat option before replacing the comma.

I was thinking about just changing the percent parser to the following, it seems to work, but then again I know you're using a different numerical format:

ts.addParser({
    id: "percent",
    is: function(s) {
        return (/\d%\)?$/).test(s);
    },
    format: function(s, table) {
        return ts.formatFloat(s.replace(/%/g, ""), table);
    },
    type: "numeric"
});

@Mottie
Copy link
Owner

Mottie commented May 11, 2012

I went ahead and made the above change to the percent parser in version 2.3.2. Please let me know if you run into any problems with it! Thanks!

@Mottie
Copy link
Owner

Mottie commented May 15, 2012

This should now be resolved, so I'm going to go ahead and close this issue. If you continue to have problems, please feel free to reopen this issue. Thanks!

@Mottie Mottie closed this as completed May 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants