Hooked on LINQ

Hooked on LINQ - Developers' Wiki
for .NET Language Integrated Query

Quick Search

Advanced Search »

OrderByDescending Operator

Modified: 2007/01/16 20:19 by t_magennis - Categorized as: LINQ to Objects
{TOC}
Namespace:System.Linq
Assembly:System.Core.dll
Extends:IEnumerable<T>

Back to Standard Query Operator Index


Edit

Introduction

The OrderBy operator orders a sequence according to one or more keys in descending order.

Edit

Method 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)



Edit

Exception

Throws an ArgumentNullException if source or keySelector is null.

Edit

Pseudo-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).

Edit

Loop 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..

Edit

Code Samples

TODO: Needs code sample.

If you would like to comment on this page, click on the Discuss button located on the top-right of each page. Feel free to edit any mistakes or ommissions you find. If you have an objection or find in-appropriate content then contact the administrator. This website is not affiliated with Microsoft®, all content and opinions are those of the specific author and some advice, solutions and article may contain un-intentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.