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

feat(orderBy): add support for custom comparators #14468

Closed

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Apr 19, 2016

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
Feature

What is the current behavior? (You can also link to an open issue here)
The comparator used by orderBy is hard-coded.

What is the new behavior (if this is a feature change)?
The user can specify a custom comparator function.

Does this PR introduce a breaking change?
No

Please check if the PR fulfills these requirements

Other information:
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 #12572 (maybe this is as close as we want to get).

Fixes #13238
Fixes #14455

Closes #5123
Closes #8112
Closes #10368

@@ -157,6 +183,38 @@ describe('Filter: orderBy', function() {
});


it('should support a custom comparator', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that the test is a bit too clever with "ownerD" being the nerd in question. ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would go as far as calling it "nerdy" 😛

I added a bunch of other tests, so it's not so necessary any more - I just kept it around because I liked it. I'm fine removing it if you think it's too confusing.

@Narretz
Copy link
Contributor

Narretz commented Apr 21, 2016

What if you want to have a custom comparator and the option to easily reverse the order?

@gkalpak gkalpak force-pushed the feat-orderBy-support-custom-comparator branch from c2cf7fe to 60dbf04 Compare April 21, 2016 23:55
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 gkalpak force-pushed the feat-orderBy-support-custom-comparator branch from 60dbf04 to 2d7bb9d Compare April 21, 2016 23:58
@gkalpak gkalpak changed the title WIP - feat(orderBy): add support for custom comparators feat(orderBy): add support for custom comparators Apr 22, 2016
@gkalpak
Copy link
Member Author

gkalpak commented Apr 22, 2016

This is now ready for review 😄

I updated the docs and added more tests. Especially wrt to how we sort things (beyond the: "numbers numerically, strings alphabetically"). I believe that if people are to rely on some ordering algorithm, they have the right to know what that is.

* order (expression is set to `'-age'`). The `comparator` is not set, which means it defaults to
* the built-in comparator.
*
<example module="orderByExample1">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you name this and the other examples (name="desriptiveName"). That makes them easier to find in the build folder.

@Narretz
Copy link
Contributor

Narretz commented Apr 22, 2016

Can you also mention in the docs that orderBy can only order alphabetically by latin characters? Actually, which characters are supported anyway?

@gkalpak
Copy link
Member Author

gkalpak commented May 26, 2016

@Narretz, I have addressed your comments. PTAL

Regarding the "supported" characters:
It uses the default < comparison (so it's not latin characters only). It is just that it doesn't do locale-sensitive comparison (and in some locales it makes a difference). The "locale-insensitive-ness" of the default comparator is already mentioned - other than that all characters are supported (in a sense).

@Narretz
Copy link
Contributor

Narretz commented May 27, 2016

Thanks for the update.
I remember we talked about how to keep the API backwards compatible in a sane way. Did you change anything about this since?

@gkalpak
Copy link
Member Author

gkalpak commented May 27, 2016

This is already backwards compatible - but you are right we did talk about something:
Making it easy to have a custom comparator and still be able to specify ascending/descending order.

We settled for orderBy(collection[, expression[, reverse[, comparator]]]). I'll make the changes.

- Decouple `reverse` from `comparator` arguments.
@gkalpak
Copy link
Member Author

gkalpak commented May 30, 2016

@Narretz, I moved comparator as a 4th argument. Now you can specify reverse and comparator independently. PTAL

* @returns {Array} Sorted copy of the source array.
* @param {boolean=} reverse - If `true`, reverse the sorting order.
* @param {(Function)=} comparator - The comparator function used to determine the relative order of
* value pairs. If omitted, a built-in comparator will be used.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there more than one built-in comparator? Or should it be "the default comparator"?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's only one. Changed it to the.

@Narretz
Copy link
Contributor

Narretz commented Jun 6, 2016

I left a few smaller comments where I think the docs could be clarified. I've also opened a PR in your repo that adds descriptions ot the examples
Otherwise LGTM. Maybe @petebacondarwin can give it another look, too.



describe('(reversing order)', function() {
it('should not reverse collection if `comparator` param is falsy',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparator param -> reverse param

@petebacondarwin
Copy link
Contributor

Apart from the couple of nitpicks in docs and tests, this LGTM.
Don't forget to fix up the commit message.

- Fix typos and wording.
@gkalpak gkalpak closed this in 48c8b23 Jun 6, 2016
gkalpak added a commit that referenced this pull request 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 Author

gkalpak commented Jun 6, 2016

Fixed and merged! Thx @Narretz and @petebacondarwin for the reviews 👍

@gkalpak gkalpak deleted the feat-orderBy-support-custom-comparator branch June 7, 2016 06:50
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

orderBy not supported persian language Ordering string which are case sensitive
4 participants