-
Notifications
You must be signed in to change notification settings - Fork 27.4k
feat(orderBy): add support for custom comparators #14468
feat(orderBy): add support for custom comparators #14468
Conversation
@@ -157,6 +183,38 @@ describe('Filter: orderBy', function() { | |||
}); | |||
|
|||
|
|||
it('should support a custom comparator', function() { |
There was a problem hiding this comment.
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. ;)
There was a problem hiding this comment.
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.
What if you want to have a custom comparator and the option to easily reverse the order? |
c2cf7fe
to
60dbf04
Compare
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
60dbf04
to
2d7bb9d
Compare
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"> |
There was a problem hiding this comment.
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.
Can you also mention in the docs that orderBy can only order alphabetically by latin characters? Actually, which characters are supported anyway? |
@Narretz, I have addressed your comments. PTAL Regarding the "supported" characters: |
Thanks for the update. |
This is already backwards compatible - but you are right we did talk about something: We settled for |
@Narretz, I moved |
* @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. |
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
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
.
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 |
|
||
|
||
describe('(reversing order)', function() { | ||
it('should not reverse collection if `comparator` param is falsy', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comparator
param -> reverse
param
Apart from the couple of nitpicks in docs and tests, this LGTM. |
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
Fixed and merged! Thx @Narretz and @petebacondarwin for the reviews 👍 |
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