Enumerable.OrderBy 方法

定义

按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order.

重载

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

根据键按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order according to a key.

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

使用指定的比较器按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order by using a specified comparer.

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>)

根据键按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order according to a key.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ OrderBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector);
public static System.Linq.IOrderedEnumerable<TSource> OrderBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector);
static member OrderBy : seq<'Source> * Func<'Source, 'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function OrderBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey)) As IOrderedEnumerable(Of TSource)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TKey

keySelector 返回的键的类型。The type of the key returned by keySelector.

参数

source
IEnumerable<TSource>

一个要排序的值序列。A sequence of values to order.

keySelector
Func<TSource,TKey>

用于从元素中提取键的函数。A function to extract a key from an element.

返回

IOrderedEnumerable<TSource>

一个 IOrderedEnumerable<TElement>,将根据键对其元素排序。An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

例外

sourcekeySelectornullsource or keySelector is null.

示例

下面的代码示例演示如何使用 OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) 对序列中的元素进行排序。The following code example demonstrates how to use OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>) to sort the elements of a sequence.

class Pet
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public static void OrderByEx1()
{
    Pet[] pets = { new Pet { Name="Barley", Age=8 },
                   new Pet { Name="Boots", Age=4 },
                   new Pet { Name="Whiskers", Age=1 } };

    IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);

    foreach (Pet pet in query)
    {
        Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
    }
}

/*
 This code produces the following output:

 Whiskers - 1
 Boots - 4
 Barley - 8
*/
Structure Pet
    Public Name As String
    Public Age As Integer
End Structure

Sub OrderByEx1()
    ' Create an array of Pet objects.
    Dim pets() As Pet = {New Pet With {.Name = "Barley", .Age = 8},
                     New Pet With {.Name = "Boots", .Age = 4},
                     New Pet With {.Name = "Whiskers", .Age = 1}}

    ' Order the Pet objects by their Age property.
    Dim query As IEnumerable(Of Pet) =
    pets.OrderBy(Function(pet) pet.Age)

    Dim output As New System.Text.StringBuilder
    For Each pt As Pet In query
        output.AppendLine(pt.Name & " - " & pt.Age)
    Next

    ' Display the output.
    Console.WriteLine(output.ToString())
End Sub

' This code produces the following output:
'
' Whiskers - 1
' Boots - 4
' Barley - 8

注解

此方法是使用延迟执行实现的。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. 此方法表示的查询在枚举对象之前不会执行,方法是通过直接调用 GetEnumerator 方法或在 Visual Basic 中使用视觉C#对象中的 foreachFor Each 来枚举对象。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.

若要按元素本身的值对序列进行排序,请为 keySelector指定标识函数(在C#视觉对象中为x => x,在 Visual Basic 中指定 Function(x) x)。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.

定义了两种方法来扩展类型 IOrderedEnumerable<TElement>,这是此方法的返回类型。Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method. 这两种方法(即 ThenByThenByDescending)允许您指定其他排序条件以对序列进行排序。These two methods, namely ThenBy and ThenByDescending, enable you to specify additional sort criteria to sort a sequence. ThenByThenByDescending 还会返回 IOrderedEnumerable<TElement>,这意味着可以对 ThenByThenByDescending 进行任意数目的连续调用。ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number of consecutive calls to ThenBy or ThenByDescending can be made.

备注

由于 IOrderedEnumerable<TElement>IEnumerable<T>继承,因此,您可以对调用 OrderByOrderByDescendingThenByThenByDescending的结果调用 OrderByOrderByDescendingBecause 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.

此方法使用默认比较器 Default来比较键。This method compares keys by using the default comparer Default.

此方法执行稳定排序;也就是说,如果两个元素的键相等,则会保留元素的顺序。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.

在查询表达式语法中,orderby (Visual C#)或 Order By (Visual Basic)子句转换为对 OrderBy的调用。In query expression syntax, an orderby (Visual C#) or Order By (Visual Basic) clause translates to an invocation of OrderBy.

另请参阅

OrderBy<TSource,TKey>(IEnumerable<TSource>, Func<TSource,TKey>, IComparer<TKey>)

使用指定的比较器按升序对序列的元素进行排序。Sorts the elements of a sequence in ascending order by using a specified comparer.

public:
generic <typename TSource, typename TKey>
[System::Runtime::CompilerServices::Extension]
 static System::Linq::IOrderedEnumerable<TSource> ^ OrderBy(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, TKey> ^ keySelector, System::Collections::Generic::IComparer<TKey> ^ comparer);
public static System.Linq.IOrderedEnumerable<TSource> OrderBy<TSource,TKey> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer);
static member OrderBy : seq<'Source> * Func<'Source, 'Key> * System.Collections.Generic.IComparer<'Key> -> System.Linq.IOrderedEnumerable<'Source>
<Extension()>
Public Function OrderBy(Of TSource, TKey) (source As IEnumerable(Of TSource), keySelector As Func(Of TSource, TKey), comparer As IComparer(Of TKey)) As IOrderedEnumerable(Of TSource)

类型参数

TSource

source 的元素类型。The type of the elements of source.

TKey

keySelector 返回的键的类型。The type of the key returned by keySelector.

参数

source
IEnumerable<TSource>

一个要排序的值序列。A sequence of values to order.

keySelector
Func<TSource,TKey>

用于从元素中提取键的函数。A function to extract a key from an element.

comparer
IComparer<TKey>

用于比较键的 IComparer<T>An IComparer<T> to compare keys.

返回

IOrderedEnumerable<TSource>

一个 IOrderedEnumerable<TElement>,将根据键对其元素排序。An IOrderedEnumerable<TElement> whose elements are sorted according to a key.

例外

sourcekeySelectornullsource 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. 此方法表示的查询在枚举对象之前不会执行,方法是通过直接调用 GetEnumerator 方法或在 Visual Basic 中使用视觉C#对象中的 foreachFor Each 来枚举对象。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.

若要按元素本身的值对序列进行排序,请为 keySelector指定标识函数(在C#视觉对象中为x => x,在 Visual Basic 中指定 Function(x) x)。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.

定义了两种方法来扩展类型 IOrderedEnumerable<TElement>,这是此方法的返回类型。Two methods are defined to extend the type IOrderedEnumerable<TElement>, which is the return type of this method. 这两种方法(即 ThenByThenByDescending)允许您指定其他排序条件以对序列进行排序。These two methods, namely ThenBy and ThenByDescending, enable you to specify additional sort criteria to sort a sequence. ThenByThenByDescending 还会返回 IOrderedEnumerable<TElement>,这意味着可以对 ThenByThenByDescending 进行任意数目的连续调用。ThenBy and ThenByDescending also return an IOrderedEnumerable<TElement>, which means any number of consecutive calls to ThenBy or ThenByDescending can be made.

备注

由于 IOrderedEnumerable<TElement>IEnumerable<T>继承,因此,您可以对调用 OrderByOrderByDescendingThenByThenByDescending的结果调用 OrderByOrderByDescendingBecause 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.

如果 null``comparer,则使用默认比较器 Default 来比较键。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.

另请参阅

适用于