IOrderedEnumerable(Of TElement).CreateOrderedEnumerable(Of TKey) Method
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
Performs a subsequent ordering on the elements of an IOrderedEnumerable(Of TElement) according to a key.
Assembly: System.Core (in System.Core.dll)
'Declaration Function CreateOrderedEnumerable(Of TKey) ( _ keySelector As Func(Of TElement, TKey), _ comparer As IComparer(Of TKey), _ descending As Boolean _ ) As IOrderedEnumerable(Of TElement)
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.
Dim fruits() As String = {"apricot", "orange", "banana", "mango", "apple", "grape", "strawberry"}
' First sort the strings by their length.
Dim sortedFruits2 As IOrderedEnumerable(Of String) = _
fruits.OrderBy(Function(fruit) fruit.Length)
' Secondarily sort the strings alphabetically, using the default comparer.
Dim sortedFruits3 As IOrderedEnumerable(Of String) = _
sortedFruits2.CreateOrderedEnumerable(Of String)( _
Function(fruit) fruit, _
System.Collections.Generic.Comparer(Of String).Default, _
False)
Dim output As New System.Text.StringBuilder
' Output the resulting sequence of strings.
For Each fruit As String In sortedFruits3
output.AppendLine(fruit)
Next
' Display the results.
outputBlock.Text &= output.ToString() & vbCrLf
' This code produces the following output:
'
' apple
' grape
' mango
' banana
' orange
' apricot
' strawberry