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
16 changes: 14 additions & 2 deletions lib/src/iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,25 @@ extension IterableSortedBy<E> on Iterable<E> {
/// Returns a new list with all elements sorted according to natural sort
/// order of the values returned by specified [selector] function.
///
/// The [ascending] parameter controls the order of sorting:
/// - Defaults to `true`, sorting elements in ascending order.
/// - Set to `false` to sort elements in descending order.
///
/// To sort by more than one property, `thenBy()` or `thenByDescending()` can
/// be called afterwards.
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
SortedList<E> sortedBy(Comparable Function(E element) selector) {
return SortedList<E>.withSelector(this, selector, 1, null);
SortedList<E> sortedBy(
Comparable Function(E element) selector, {
bool ascending = true,
}) {
return SortedList<E>.withSelector(
this,
selector,
ascending ? 1 : -1,
null,
);
}
}

Expand Down
17 changes: 15 additions & 2 deletions lib/src/sorted_list.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:math';

import 'package:dartx/dartx.dart';

Comparator<E> _getComparator<E>(
Expand Down Expand Up @@ -43,10 +44,22 @@ class SortedList<E> extends _DelegatingList<E> {
/// defined order and natural sort order of the values returned by specified
/// [selector] function.
///
/// The [ascending] parameter controls the order of sorting:
/// - Defaults to `true`, sorting elements in ascending order.
/// - Set to `false` to sort elements in descending order.
///
/// **Note:** The actual sorting is performed when an element is accessed for
/// the first time.
SortedList<E> thenBy(Comparable Function(E element) selector) {
return SortedList<E>.withSelector(this, selector, 1, _comparator);
SortedList<E> thenBy(
Comparable Function(E element) selector, {
bool ascending = true,
}) {
return SortedList<E>.withSelector(
this,
selector,
ascending ? 1 : -1,
_comparator,
);
}

/// Returns a new list with all elements sorted according to previously
Expand Down
16 changes: 16 additions & 0 deletions test/sorted_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ void main() {
test('sort', () {
expect([4, 2, 1, 3].sortedBy((it) => it), [1, 2, 3, 4]);
});

test('sort, ascending', () {
expect([4, 2, 1, 3].sortedBy((it) => it, ascending: true), [1, 2, 3, 4]);
expect([4, 2, 1, 3].sortedBy((it) => it, ascending: false), [4, 3, 2, 1]);
});

test('thenBy', () {
final list = ['ba', 'c', 'aa'].sortedBy((it) => it.length);
expect(list, ['c', 'ba', 'aa']);
Expand All @@ -19,6 +25,16 @@ void main() {
final thenByDesc = list.thenByDescending((it) => it);
expect(thenByDesc, ['c', 'ba', 'aa']);
});

test('thenBy, ascending', () {
final list = ['ba', 'c', 'aa'].sortedBy((it) => it.length);
expect(list, ['c', 'ba', 'aa']);
final thenBy = list.thenBy((it) => it, ascending: true);
expect(thenBy, ['c', 'aa', 'ba']);
final thenByDesc = list.thenBy((it) => it, ascending: false);
expect(thenByDesc, ['c', 'ba', 'aa']);
});

group('_DelegatingIterable', () {
test('any', () {
expect(_sortedList.any((it) => it > 2), isTrue);
Expand Down