{TOC}
| Namespace: | System.Linq |
| Assembly: | System.Core.dll |
| Extends: | IEnumerable<T> |
Back to
Standard Query Operator IndexEditIntroduction
The OrderBy operator orders a sequence according to one or more keys in descending order.
EditMethod Signatures
// 1 - Order the sequence in reverse alphabetical order.
public static IOrderedSequence<TSource> OrderByDescending<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
// 2 - Order the sequence in reverse order. Use a custom comparer function.
public static IOrderedSequence<TSource> OrderByDescending<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector,
IComparer<TKey> comparer)EditException
Throws an ArgumentNullException if
source or
keySelector is null.
EditPseudo-code
Overload 1
If
source is null, throw an ArgumentNullException.
If
keySelector is null, throw an ArgumentNullException.
Iterate the source sequence
Copy the current element into a buffer collection.
Iterate the buffer sequence.
Evaluate the keys of each element using keySelector(current element)
Sort the buffer collection of keys in reverse order using
Quicksort algorithm.
Each key is evaluated greater, equal or less than by the Comparer<TKey>.Default; comparer function and then reversed in sign to affect a reverse ordering sequence.
Iterate the buffer collection, now sorted.
Return the current element. (Resume execution from here when the next element is requested).
Overload 2 - custom comparer.
If
source is null, throw an ArgumentNullException.
If
keySelector is null, throw an ArgumentNullException.
If
comparer is null, use
Comparer<TKey>.Default; instead.
Iterate the source sequence.
Copy the current element into a buffer collection.
Iterate the buffer sequence.
Evaluate the keys of each element using keySelector(current element).
Sort the buffer collection of keys in reverse order using
Quicksort algorithm.
Each key is evaluated greater, equal or less than by the custom comparer function and then reversed in sign to affect a reverse ordering sequence.
Iterate the buffer collection, now sorted.
Return the current element. (Resume execution from here when the next element is requested).
EditLoop Count
2 - 2.5. A copy of the source elements is made, the keys evaluated, then the sequence is sorted by a Quicksort algorithm. This operator implements the standard
deferred execution iterator pattern. This means, no looping will occur until the result is iterated over..
EditCode Samples
TODO: Needs code sample.