mattiash / angular-tablesort

Sort angularjs tables easily
MIT License
184 stars 66 forks source link

Support for numeric collator #73

Closed parttee closed 7 years ago

parttee commented 7 years ago

Added support for comparison between numeric values. If both compared values are numeric, those should be compared with Intl.Collator and option "numeric:true"

Issue https://github.com/mattiash/angular-tablesort/issues/72

mattiash commented 7 years ago

If typeof aval === 'number', then it means that the value is a pure number (i.e. not a string containing a number) and then you can compare it with a simple compare-function. Intl.Collator with option numeric:true is for when you have strings that contain numbers.

Your patch will most likely work since the number is probably automatically converted to a string when you pass it to compare(), but it is a very inefficient way of comparing numbers.

parttee commented 7 years ago

That's correct. Instead of typeof check it should be "isNumeric" check

ChrisMBarr commented 7 years ago

We've run into this issue as well and would ❤️ to see this merged in. We are using <th ts-criteria="productCount | parseInt"> but all the number just come back sorted alphabetically instead of numerically.

Here's the sort order we are currently seeing for an ascending sort:

0, 1, 1, 1, 16, 2, 25, 3, 42

Obviously, the expected order would be

0, 1, 1, 1, 2, 3, 16, 25, 42
mattiash commented 7 years ago

I don't think the current patch is correct as I described in https://github.com/mattiash/angular-tablesort/pull/73#issuecomment-257116578. Ideally it should check if typeof is number and do a numeric compare (a<b), if isNumeric is true it should use the numeric:true collator and otherwise use the current collator.

mattiash commented 7 years ago

Also see #68 which is bound to be in conflict with this PR

mattiash commented 7 years ago

Closed since PR #77 has been merged instead.