Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 8 additions & 4 deletions pkgs/collection/lib/src/iterable_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Copy link
Member

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, every sort 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.)

/// when [ascending] is `false`.
List<T> sortedBy<K extends Comparable<K>>(
K Function(T element) keyOf, {
bool ascending = true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer to name it descending, so the default is false.
Just because it's usually best to have "nothing" mean false.
Then it feels like you are opting in to the non-default behavior. Using ascending: false feels ... conflicted. You are opting out of the default behavior, but that doesn't directly say what it opts in to.

}) {
int compare(K a, K b) => ascending ? a.compareTo(b) : b.compareTo(a);
Copy link
Member

Choose a reason for hiding this comment

The 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);

Copy link
Member

Choose a reason for hiding this comment

The 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.
That said: I don't want to do the branch on every comparison, so something like what @natebosch says is better.

var elements = [...this];
mergeSortBy<T, K>(elements, keyOf, compareComparable);
mergeSortBy<T, K>(elements, keyOf, compare);
return elements;
}

Expand Down
10 changes: 10 additions & 0 deletions pkgs/collection/test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ void main() {
test('multiple', () {
expect(iterable(<int>[3, 20, 100]).sortedBy(toString), [100, 20, 3]);
});
test('multiple ascending', () {
expect(
iterable(<int>[3, 20, 100]).sortedBy(toString, ascending: true),
[100, 20, 3]);
});
test('multiple descending', () {
expect(
iterable(<int>[3, 20, 100]).sortedBy(toString, ascending: false),
[3, 20, 100]);
});
});
group('.sortedByCompare', () {
test('empty', () {
Expand Down
Loading