-
Notifications
You must be signed in to change notification settings - Fork 19
add ascending
to IterableExtension.sortedBy
#731
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ import 'dart:math' show Random; | |
|
||
import 'algorithms.dart'; | ||
import 'functions.dart' as functions; | ||
import 'utils.dart'; | ||
|
||
/// Extensions that apply to all iterables. | ||
/// | ||
|
@@ -71,10 +70,15 @@ extension IterableExtension<T> on Iterable<T> { | |
/// Creates a sorted list of the elements of the iterable. | ||
/// | ||
/// The elements are ordered by the natural ordering of the | ||
/// property [keyOf] of the element. | ||
List<T> sortedBy<K extends Comparable<K>>(K Function(T element) keyOf) { | ||
/// [keyOf] property when [ascending] is `true` or reverse ordering | ||
/// when [ascending] is `false`. | ||
List<T> sortedBy<K extends Comparable<K>>( | ||
K Function(T element) keyOf, { | ||
bool ascending = true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I would prefer to name it |
||
}) { | ||
int compare(K a, K b) => ascending ? a.compareTo(b) : b.compareTo(a); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At the risk of premature optimization, I think we should keep this as a static tear-off instead of a new lambda on every use. If we decide to go forward with this change I think something like final compare = ascending ? compareComparable : compareComparableDescending;
int compareComparableDescending<T extends Comparable<T>>(T a, T b) => b.compareTo(a); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's typed, it's a new closure on every use anyway, because generic instantiation also causes a new closure. |
||
var elements = [...this]; | ||
mergeSortBy<T, K>(elements, keyOf, compareComparable); | ||
mergeSortBy<T, K>(elements, keyOf, compare); | ||
return elements; | ||
} | ||
|
||
|
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.
The
sorted
function should get the same behavior. Really, everysort
function should behave the same way if at all possible.(It's probably impossible because there is some
sort([int Function(T, T) compare])
somewhere which prevents a named parameter.)