{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 ascending order.
EditMethod Signatures
// 1 - Order by keySelector
public static IOrderedSequence<TSource> OrderBy<TSource, TKey>(
this IEnumerable<TSource> source,
Func<TSource, TKey> keySelector)
// 2 - Order by keySelector using custom comparer
public static IOrderedSequence<TSource> OrderBy<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 using
Quicksort algorithm.
Each key is evaluated greater, equal or less than by the Comparer<TKey>.Default; comparer function.
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 using
Quicksort algorithm.
Each key is evaluated greater, equal or less than by the custom comparer function.
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 via 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.