IOrderedEnumerable<TElement>.CreateOrderedEnumerable<TKey> 方法

定義

依據索引鍵,在 IOrderedEnumerable<TElement> 的項目上執行後續的排序。

public:
generic <typename TKey>
 System::Linq::IOrderedEnumerable<TElement> ^ CreateOrderedEnumerable(Func<TElement, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer, bool descending);
public System.Linq.IOrderedEnumerable<TElement> CreateOrderedEnumerable<TKey> (Func<TElement,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer, bool descending);
public System.Linq.IOrderedEnumerable<out TElement> CreateOrderedEnumerable<TKey> (Func<out TElement,TKey> keySelector, System.Collections.Generic.IComparer<TKey>? comparer, bool descending);
public System.Linq.IOrderedEnumerable<out TElement> CreateOrderedEnumerable<TKey> (Func<out TElement,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer, bool descending);
abstract member CreateOrderedEnumerable : Func<'Element, 'Key> * System.Collections.Generic.IComparer<'Key> * bool -> System.Linq.IOrderedEnumerable<'Element>
Public Function CreateOrderedEnumerable(Of TKey) (keySelector As Func(Of TElement, TKey), comparer As IComparer(Of TKey), descending As Boolean) As IOrderedEnumerable(Of TElement)
Public Function CreateOrderedEnumerable(Of TKey) (keySelector As Func(Of Out TElement, TKey), comparer As IComparer(Of TKey), descending As Boolean) As IOrderedEnumerable(Of Out TElement)

類型參數

TKey

keySelector 所產生的索引鍵型別。

參數

keySelector
Func<TElement,TKey>

Func<T,TResult>,用來擷取每個項目的索引鍵。

comparer
IComparer<TKey>

IComparer<T>,用來比較索引鍵,以便在傳回的序列中放置。

descending
Boolean

true 表示依遞減順序排序項目,false 表示依遞增順序排序項目。

傳回

依據索引鍵排序其項目的 IOrderedEnumerable<TElement>

範例

下列程式代碼範例示範如何使用 CreateOrderedEnumerable 在上 IOrderedEnumerable<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
' 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(ByVal fruit) fruit.Length)
' Secondarily sort the strings alphabetically, using the default comparer.
Dim sortedFruits3 As IOrderedEnumerable(Of String) = _
    sortedFruits2.CreateOrderedEnumerable(Of String)( _
        Function(ByVal 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.
MsgBox(output.ToString())

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

備註

這個方法所提供的功能類似於 或 所提供的ThenBy功能,視 是 descendingtrueThenByDescendingfalse而定。 兩者都會執行已排序之型 IOrderedEnumerable<TElement>別 的次級順序。

適用於