-
Notifications
You must be signed in to change notification settings - Fork 34
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,16 @@ 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, | ||
|
Member
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
Author
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 understand the concern about double negatives with
That said, I recognize the clarity benefit of |
||
| }) { | ||
| final compare = | ||
| ascending ? (K a, K b) => a.compareTo(b) : (K a, K b) => b.compareTo(a); | ||
| var elements = [...this]; | ||
| mergeSortBy<T, K>(elements, keyOf, compareComparable); | ||
| mergeSortBy<T, K>(elements, keyOf, compare); | ||
| return elements; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.