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的功能,具体取决于 是 descendingtrue 还是 falseThenByDescending 它们都对类型 IOrderedEnumerable<TElement>已排序的序列执行从属排序。

适用于