-
Notifications
You must be signed in to change notification settings - Fork 8
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
Add IterableExtension.sortedByDescending
to package:collection
#725
Comments
Don't want to have every version of sorting also in the reverse version. class Comparable<T> {
// ...
static int Function(T, T) compareInverse<T extends Comparable<T>>(T v1, T v2) => v2.compareTo(v1);
} so you can just do: issued.sortedByCompare((issue) => issue.updatedAt, Comparable.compareInverse); and not have to write the compare function, just a (longer) name. |
Could you expand why not?
This seems like it'd be useful too. However, this solution is more verbose and harder to discover. |
I'm just of the opinion that having a shorthand for every recognizable combination of basic operations leads to a cluttered API. It's a never-ending process to add more and more specialized variants, a lot of which see little use and don't really carry their own weight. Giving an efficient version of the general functionality, and letting people combine efficient primitives into the combination they need, is a better level of abstraction. That's just, like, my opinion. Maybe it's a philosophy born from having a very small team to build and support these libraries originally. (And there is still a limited amount of resources for external libraries). |
In a previous life, I worked on .NET which has My experience is that these APIs are not at all error prone. I can't recall a single instance where
GitHub search indicates heavy use for both of these .NET APIs: 963K results for
I agree exposing more primitives that can be combined is a good idea. I'm not opposed to doing both :) However, if a newbie wants to sort a list in reverse, how would they discover On the other hand, I suspect discovering |
We have four variants of Ascending/Descending direction is a common customization of sorting. It should no be so hard. I agree that adding four new method names is not a great idea.
We could have a comparator transformer to reverse sort order - that would be 'compositional' and reduce the combination space, but for many developers, it would seem arcane. One might argue that the I don't think the better static errors make None of these methods mention stability in their documentation, yet some are implemented in terms of The two definitions with the same name I would suggest having only two extension methods, I would still add |
Maybe sorting in the opposite order happens often enough that operations that do the same thing in the inverse order would be reasonable. (And maybe a named boolean is easier than coming up with new names, at least if none of the functions take an optional positional argument yet.) I'm not too worried about a sorting function doing one extra
It's baffling every time But then we come to naming: |
Problem
To sort
GitHubIssue
s by theirupdatedAt
field, descending:This is inconvenient.
Solution
Introduce a
sortedByDescending
helper:The text was updated successfully, but these errors were encountered: