Hooked on LINQ

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

Quick Search

Advanced Search »
{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 ascending order.

Edit

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



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

Edit

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

Edit

Code Samples

var qName = from n in db.tblNames
where n.DeletedFlag == false
orderby n.LastName,n.FirstName
select n;

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 omissions 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 unintentional errors - please use care. Powered by ScrewTurn Wiki version 2.0.33. Some of the icons created by FamFamFam.