-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add Collection#sorted, Collection#min, Collection#max #1319
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
Comments
Removed Type-Defect label. |
This comment was originally written by @seaneagan * If no self-mutating List#sort is needed, could rename Collection#sorted to Collection#sort
E min<T>([int compare(E a, E b)]) { basically assume transitivity of the comparator(s) (in order to get O(n) performance), and when two items compare equal, keep the earliest iterated one. |
A key function is often more convenient that a comparator. I like Python's min/max functions. var strs = ['short', 'medium', 'longest']; |
This comment was originally written by @seaneagan I'm thinking just have them take one dynamic argument, which can either be a Comparator or a key function which might be defined as: typedef num Metric<E>(E e); Then if/when we get union types, this single argument can be typed as a class Iterable<E> { class Stream<E> { |
Issue #7430 has been merged into this issue. |
This comment was originally written by @seaneagan Looks like min and max were added to lib_v2 in https://codereview.chromium.org/11727007/. Thanks! Is lazy sorting of Iterables and Streams, i.e. the "sorted" method above, also planned? do we need a separate issue for it? |
Lazy sorting is not planned. The few operations that can be done without sorting (length, first -> min, last -> max) are not really worth the complexity. Also, it will be a dangerous "view", because you will need to sort for most operations, and any change to the underlying list will mean you need to sort again. With cascades, it's not bad to do: (list.toList()..sort(comparator)) to get a new sorted list. The min and max methods were removed again from Iterable. Maybe we should add min/max functions to comparable: Then we can find the minimum of a list of comparables as: |
(list.toList()..sort(comparator)) isn't too bad in terms of code length, but it's not going to be an obvious choice to anyone new to the language. |
This comment was originally written by @seaneagan Static methods would definitely be better than nothing, but you lose the |
Would a method named toSortedList([compare]) make sense? |
toSortedList() would work, but imho it's not worth it. It doesn't really buy much compared to toList()..sort(). Yes toList()..sort() is not completely obvious but the next best thing is: while this is slightly longer it isn't that bad and is readable. Added AsDesigned label. |
This comment was originally written by @mezoni Lazy sorting. Result is IQueryable. import 'package:queries/collections.dart'; void main() { Output: Sorting algorithm is stable (Symmerge sort). |
???
|
This issue was originally filed by @seaneagan
List#sort is useful for sorting a list in place, but often you don't want to mutate the list, but rather get a sorted copy of it, and also any collection can be sorted, not just lists, but right now you have to do something like:
List sortedList = new List.from(collection);
sortedList.sort(comparator);
which is inconvenient, but more importantly means that all the elements must be sorted (slow) even to do basic calculations on the returned list, such as "list.last()". Thus, it would be nice to have a separate sorting method in Collection which returns a sorted "view" (see issue #1235) of the Collection:
// comparator optional (see issue #1305)
List<E> sorted([int compare(E a, E b)]);
The two most common use cases for sorting are to calculate a "min" or "max", so those would be nice to have in Collection as well:
E min([int compare(E a, E b)]); // => sorted(compare)[0]
E max([int compare(E a, E b)]) // => sorted(compare).last()
The text was updated successfully, but these errors were encountered: