Windows apps
Collapse the table of content
Expand the table of content
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Enumerable.OrderBy<TSource, TKey> Method (IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>)

Sorts the elements of a sequence in ascending order by using a specified comparer.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)

public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(
	this IEnumerable<TSource> source,
	Func<TSource, TKey> keySelector,
	IComparer<TKey> comparer

Type Parameters


The type of the elements of source.


The type of the key returned by keySelector.


Type: System.Collections.Generic.IEnumerable<TSource>
A sequence of values to order.
Type: System.Func<TSource, TKey>
A function to extract a key from an element.
Type: System.Collections.Generic.IComparer<TKey>
An IComparer<T> to compare keys.

Return Value

Type: System.Linq.IOrderedEnumerable<TSource>
An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type IEnumerable<TSource>. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).


source or keySelector is null.

This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its GetEnumerator method directly or by using foreach in Visual C# or For Each in Visual Basic.

To order a sequence by the values of the elements themselves, specify the identity function (x => x in Visual C# or Function(x) x in Visual Basic) for keySelector.

Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method. These two methods, namely ThenBy and ThenByDescending, enable you to specify additional sort criteria to sort a sequence. ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number of consecutive calls to ThenBy or ThenByDescending can be made.


Because IOrderedEnumerable<TElement> inherits from IEnumerable<T>, you can call OrderBy or OrderByDescending on the results of a call to OrderBy, OrderByDescending, ThenBy or ThenByDescending. Doing this introduces a new primary ordering that ignores the previously established ordering.

If comparer is null, the default comparer Default is used to compare keys.

This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved. In contrast, an unstable sort does not preserve the order of elements that have the same key.

The following code example demonstrates how to use OrderBy<TSource, TKey>(IEnumerable<TSource>, Func<TSource, TKey>, IComparer<TKey>) to sort the elements of an array.

// First, declare a few classes that implement the IComparer interface.

public class CompareStrings : IComparer<string>
    // Because the class implements IComparer, it must define a 
    // Compare method. The method returns a signed integer that indicates 
    // whether s1 > s2 (return is greater than 0), s1 < s2 (return is negative),
    // or s1 equals s2 (return value is 0). This Compare method compares strings. 
    public int Compare(string s1, string s2)
        return string.Compare(s1, s2, true);

public class CompareIntegers : IComparer<int>
    // Because the class implements IComparer, it must define a 
    // Compare method. This Compare method compares integers.
    public int Compare(int i1, int i2)
        return i1 - i2;

// The following method tests the Compare methods defined in the previous classes.

public static void OrderByIComparer()
    string[] unsortedArray = { "three", "six", "nine", "twelve", "fifteen", "eighteen" };

     // Sort the elements of the array alphabetically.
    var sortedArray = unsortedArray.OrderBy(a => a, new CompareStrings());

    Console.WriteLine("Array elements in alphabetical order:");
    foreach (var element in sortedArray)

    // Change the lambda expression to sort by the length of each string.
    sortedArray = unsortedArray.OrderBy(a => a.Length, new CompareIntegers());

    Console.WriteLine("\nArray elements sorted by the lengths of the strings:");
    foreach (var element in sortedArray)

// Output:
// Array elements in alphabetical order:
// eighteen
// fifteen
// nine
// six
// three
// twelve

// Array elements sorted by the lengths of the strings:
// six
// nine
// three
// twelve
// fifteen
// eighteen

.NET Framework

Supported in: 4, 3.5

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

© 2018 Microsoft