orderByDescending<TKey> method
- TKey keySelector(
- T element
- EqualityComparer<
TKey> ? keyComparer,
Sorts the iteration in descending (greatest-to-least) order.
First, orderByDescending iterates over the entire iterable, creating a
list of keys generated by keySelector
associated to their corresponding
elements. Then a QuickSort algorithm is applied to the keys, using the
sort property in keyComparer
to determine sort order. Afterwards,
the resulting iterable will consist of the elements in the source
iterable in the order determined by the sorted list of keys.
When the type of the iterable is one of the below types, the EqualityComparer can be omitted. In this case, the function defaults to predefined minimum functions depending on the type:
- Numeric types (
num
,int
,double
) will be sorted by their values in descending order. String
types will be sorted in reverse-alphabetic order.
If the iterable type is not one of these types and the EqualityComparer is not provided, the order of the resulting iterable is unpredictable.
If the iterable is already sorted in descending order, the resulting iterable will be unchanged.
Example:
void main() {
final list = [4, 3, 5, 2, 1];
final result = list.orderByDescending((x) => x);
// Result: [5, 4, 3, 2, 1]
}
Implementation
Iterable<T> orderByDescending<TKey>(
TKey Function(T element) keySelector, {
EqualityComparer<TKey>? keyComparer,
}) {
keyComparer ??= EqualityComparer.forType<TKey>();
return InternalOrderedIterable(this, keySelector, keyComparer, true);
}