Skip to content
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

docs: Add a warning about dart-lang/sdk#43763 to sortedBy #315

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ extension IterableExtension<T> on Iterable<T> {
///
/// The elements are ordered by the natural ordering of the
/// property [keyOf] of the element.
///
/// Due to https://github.com/dart-lang/sdk/issues/43763
/// if you are comparing using int, you will need to use the `num` type
/// annotation instead, e.g.
///
/// ```dart
/// class Foo {
/// final int bar;
/// Foo(this.bar);
/// }
///
/// final foos = [Foo(1), Foo(2), Foo(3)];
/// // final sortedFoos = foos.sortedBy((foo) => foo.bar); // Error
/// final sortedFoos = foos.sortedBy((foo) => foo.bar as num); // Works
/// final alsoWorks = foos.sortedBy<num>((foo) => foo.bar); // Works
/// ```
List<T> sortedBy<K extends Comparable<K>>(K Function(T element) keyOf) {
var elements = [...this];
mergeSortBy<T, K>(elements, keyOf, compareComparable);
Expand Down