Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

feat(orderBy): Support natural sort #12572

Closed
gabrielmaldi opened this issue Aug 13, 2015 · 6 comments
Closed

feat(orderBy): Support natural sort #12572

gabrielmaldi opened this issue Aug 13, 2015 · 6 comments

Comments

@gabrielmaldi
Copy link
Contributor

This is a feature request. I searched issues and pull request and didn't find anything, sorry if this is a dupe.

I think that many of us would like to be able to ask orderBy to sort strings in a "natural" way (i.e. foo 92 "is smaller than" foo 147, as opposed to the current lexicographical order which sorts 1 before 9).

I'd be willing to submit a pull request but first I'd like to know if this is a feature the Angular team would like to add, discuss the possible syntax, and get some guidelines (I took a look at orderByFilter and noticed it's really optimized for performance).

Thanks!

@lgalfaso
Copy link
Contributor

This is one of those things and always found a reasonable workaround (eg
for this case, (+somefield.substr(4))), but would be open to it if there is
enough community interest

@Ericson578
Copy link

Maybe a new filter that converts to integer before ordering? orderByInt()

@fhernandezn
Copy link

@Narretz I'd like to know your oppinion about this feature, I could create a new filter.

@Ericson578
Copy link

I've definitely run into this problem before, where I have a field that I want to order by in an ng-repeat, and the field is a number but for a variety of reasons is stored in js as a string. I've fixed the problem by writing a custom filter that converts to int before comparing using parseInt(num, 2), but would be nice if there was already a filter that did this.

@gkalpak
Copy link
Member

gkalpak commented Oct 26, 2015

Is it just me or does this sound like a better fit for an external module (rather than a core feature) ?

gkalpak added a commit to gkalpak/angular.js that referenced this issue Apr 19, 2016
With this change the 3rd argument is interpreted as a comparator function, used to compare the
values returned by the predicates. Leaving it empty falls back to the default comparator and a
value of `false` is a special case that also falls back to the default comparator but reverses the
order. Thus the new implementation is backwards compatible.

Helps with angular#12572 (maybe this is as close as we want to get).

Fixes angular#13238
Fixes angular#14455

Closes angular#5123
Closes angular#8112
Closes angular#10368
gkalpak added a commit to gkalpak/angular.js that referenced this issue Apr 21, 2016
With this change the 3rd argument is interpreted as a comparator function, used to compare the
values returned by the predicates. Leaving it empty falls back to the default comparator and a
value of `false` is a special case that also falls back to the default comparator but reverses the
order. Thus the new implementation is backwards compatible.

This commit also xpands the documentation to cover the algorithm used to sort elements and adds
a few more unit and e2e tests (unrelated to the change).

Helps with angular#12572 (maybe this is as close as we want to get).

Fixes angular#13238
Fixes angular#14455

Closes angular#5123
Closes angular#8112
Closes angular#10368
gkalpak added a commit to gkalpak/angular.js that referenced this issue Apr 21, 2016
With this change the 3rd argument is interpreted as a comparator function, used to compare the
values returned by the predicates. Leaving it empty falls back to the default comparator and a
value of `false` is a special case that also falls back to the default comparator but reverses the
order. Thus the new implementation is backwards compatible.

This commit also expands the documentation to cover the algorithm used to sort elements and adds
a few more unit and e2e tests (unrelated to the change).

Helps with angular#12572 (maybe this is as close as we want to get).

Fixes angular#13238
Fixes angular#14455

Closes angular#5123
Closes angular#8112
Closes angular#10368
gkalpak added a commit that referenced this issue Jun 6, 2016
Add an optional, 4th argument (`comparator`) for specifying a custom comparator function, used to
compare the values returned by the predicates. Omitting the argument, falls back to the default,
built-in comparator. The 3rd argument (`reverse`) can still be used for controlling the sorting
order (i.e. ascending/descending).

Additionally, the documentation has been expanded to cover the algorithm used by the built-in
comparator and a few more unit and e2e tests (unrelated to the change) have been added.

Helps with #12572 (maybe this is as close as we want to get).

Fixes #13238
Fixes #14455

Closes #5123
Closes #8112
Closes #10368

Closes #14468
gkalpak added a commit that referenced this issue Jun 6, 2016
Add an optional, 4th argument (`comparator`) for specifying a custom comparator function, used to
compare the values returned by the predicates. Omitting the argument, falls back to the default,
built-in comparator. The 3rd argument (`reverse`) can still be used for controlling the sorting
order (i.e. ascending/descending).

Additionally, the documentation has been expanded to cover the algorithm used by the built-in
comparator and a few more unit and e2e tests (unrelated to the change) have been added.

Helps with #12572 (maybe this is as close as we want to get).

Fixes #13238
Fixes #14455

Closes #5123
Closes #8112
Closes #10368

Closes #14468
@gkalpak
Copy link
Member

gkalpak commented Jun 6, 2016

Now that #14468 has landed in master, it will be even easier to do this by providing a custom comparator function. I'm going to close this for now, since it seems like a better fit for an external module.

@gkalpak gkalpak closed this as completed Jun 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants