This topic has not yet been rated Rate this topic

IOrderedEnumerable(Of TElement).CreateOrderedEnumerable(Of TKey) Method

Performs a subsequent ordering on the elements of an IOrderedEnumerable(Of TElement) according to a key.

Namespace:  System.Linq
Assembly:  System.Core (in System.Core.dll)
IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey>(
	Func<TElement, TKey> keySelector,
	IComparer<TKey> comparer,
	bool descending
)

Type Parameters

TKey

The type of the key produced by keySelector.

Parameters

keySelector
Type: System.Func(Of TElement, TKey)
The Func(Of T, TResult) used to extract the key for each element.
comparer
Type: System.Collections.Generic.IComparer(Of TKey)
The IComparer(Of T) used to compare keys for placement in the returned sequence.
descending
Type: System.Boolean
true to sort the elements in descending order; false to sort the elements in ascending order.

Return Value

Type: System.Linq.IOrderedEnumerable(Of TElement)
An IOrderedEnumerable(Of TElement) whose elements are sorted according to a key.

The functionality provided by this method is like that provided by ThenBy or ThenByDescending, depending on whether descending is true or false. They both perform a subordinate ordering of an already sorted sequence of type IOrderedEnumerable(Of TElement).

The following code example demonstrates how to use CreateOrderedEnumerable(Of TKey) to perform a secondary ordering on an IOrderedEnumerable(Of TElement).


            // Create an array of strings to sort.
            string[] fruits = { "apricot", "orange", "banana", "mango", "apple", "grape", "strawberry" };
            // First sort the strings by their length.
            IOrderedEnumerable<string> sortedFruits2 =
                fruits.OrderBy(fruit => fruit.Length);
            // Secondarily sort the strings alphabetically, using the default comparer.
            IOrderedEnumerable<string> sortedFruits3 =
                sortedFruits2.CreateOrderedEnumerable<string>(
                    fruit => fruit,
                    Comparer<string>.Default, false);

            // Output the resulting sequence of strings.
            foreach (string fruit in sortedFruits3)
                Console.WriteLine(fruit);

            // This code produces the following output:
            //
            // apple
            // grape
            // mango
            // banana
            // orange
            // apricot
            // strawberry



.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.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ