Skip to content
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
13 changes: 9 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,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,
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.

Copy link
Author

Choose a reason for hiding this comment

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

I understand the concern about double negatives with ascending: false. However, I'd lean towards ascending: true in this case because:

  1. Flutter's DataTable already uses sortAscending: true as the default, and DataColumnSortCallback uses bool ascending in its signature
  2. In sorting contexts across Flutter/Dart, I feel developers would expect the ascending parameter

That said, I recognize the clarity benefit of descending: true for opt-in behavior. If you still prefer descending: false after considering these points, I'm happy to update it accordingly. :)

}) {
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;
}

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 @@ -70,6 +70,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