From fd91b6721332cf19610e5b9b034d4ad788189c8d Mon Sep 17 00:00:00 2001 From: Arthur Miranda Date: Thu, 5 Dec 2024 21:49:42 -0300 Subject: [PATCH 1/2] add `ascending` to IterableExtension.sortedBy --- pkgs/collection/lib/src/iterable_extensions.dart | 12 ++++++++---- pkgs/collection/test/extensions_test.dart | 10 ++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/pkgs/collection/lib/src/iterable_extensions.dart b/pkgs/collection/lib/src/iterable_extensions.dart index 10f467608..6c0606952 100644 --- a/pkgs/collection/lib/src/iterable_extensions.dart +++ b/pkgs/collection/lib/src/iterable_extensions.dart @@ -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 on Iterable { /// 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 sortedBy>(K Function(T element) keyOf) { + /// [keyOf] property when [ascending] is `true` or reverse ordering + /// when [ascending] is `false`. + List sortedBy>( + K Function(T element) keyOf, { + bool ascending = true, + }) { + int compare(K a, K b) => ascending ? a.compareTo(b) : b.compareTo(a); var elements = [...this]; - mergeSortBy(elements, keyOf, compareComparable); + mergeSortBy(elements, keyOf, compare); return elements; } diff --git a/pkgs/collection/test/extensions_test.dart b/pkgs/collection/test/extensions_test.dart index 6c5b45f14..e7eb24551 100644 --- a/pkgs/collection/test/extensions_test.dart +++ b/pkgs/collection/test/extensions_test.dart @@ -64,6 +64,16 @@ void main() { test('multiple', () { expect(iterable([3, 20, 100]).sortedBy(toString), [100, 20, 3]); }); + test('multiple ascending', () { + expect( + iterable([3, 20, 100]).sortedBy(toString, ascending: true), + [100, 20, 3]); + }); + test('multiple descending', () { + expect( + iterable([3, 20, 100]).sortedBy(toString, ascending: false), + [3, 20, 100]); + }); }); group('.sortedByCompare', () { test('empty', () { From c63dff2daed25592daf7e7fcfb46335d6834a6c6 Mon Sep 17 00:00:00 2001 From: Arthur Miranda Date: Mon, 10 Nov 2025 02:18:42 +0100 Subject: [PATCH 2/2] Improve compare function in sortedBy extension --- pkgs/collection/lib/src/iterable_extensions.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/collection/lib/src/iterable_extensions.dart b/pkgs/collection/lib/src/iterable_extensions.dart index 6c0606952..ae5bb52ca 100644 --- a/pkgs/collection/lib/src/iterable_extensions.dart +++ b/pkgs/collection/lib/src/iterable_extensions.dart @@ -76,7 +76,8 @@ extension IterableExtension on Iterable { K Function(T element) keyOf, { bool ascending = true, }) { - int compare(K a, K b) => ascending ? a.compareTo(b) : b.compareTo(a); + final compare = + ascending ? (K a, K b) => a.compareTo(b) : (K a, K b) => b.compareTo(a); var elements = [...this]; mergeSortBy(elements, keyOf, compare); return elements;