对数据进行排序

更新:2007 年 11 月

排序操作按一个或多个属性对序列的元素进行排序。第一个排序条件对元素执行主要排序。通过指定第二个排序条件,可以对各个主要排序组中的元素进行排序。

下图演示对一个字符序列执行按字母排序操作的结果。

LINQ 排序操作

下面一节中列出了对数据进行排序的标准查询运算符方法。

方法

方法名

说明

C# 查询表达式语法

Visual Basic 查询表达式语法

更多信息

OrderBy

按升序对值进行排序。

orderby

Order By

Enumerable.OrderBy

Queryable.OrderBy

OrderByDescending

按降序对值进行排序。

orderby … descending

Order By … Descending

Enumerable.OrderByDescending

Queryable.OrderByDescending

ThenBy

按升序执行次要排序。

orderby …, …

Order By …, …

Enumerable.ThenBy

Queryable.ThenBy

ThenByDescending

按降序执行次要排序。

orderby …, … descending

Order By …, … Descending

Enumerable.ThenByDescending

Queryable.ThenByDescending

Reverse

颠倒集合中的元素的顺序。

不适用。

不适用。

Enumerable.Reverse<TSource>

Queryable.Reverse<TSource>

查询表达式语法示例

主要排序示例

主要升序排序

下面的示例演示如何在 LINQ 查询中使用 orderby(在 Visual Basic 中为 Order By)子句来按字符串长度对数组中的字符串进行升序排序。

Dim words() As String = {"the", "quick", "brown", "fox", "jumps"}

Dim sortQuery = From word In words _
                Order By word.Length _
                Select word

Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' the
' fox
' quick
' brown
' jumps

string[] words = { "the", "quick", "brown", "fox", "jumps" };

IEnumerable<string> query = from word in words
                            orderby word.Length
                            select word;

foreach (string str in query)
    Console.WriteLine(str);

/* This code produces the following output:

    the
    fox
    quick
    brown
    jumps
*/

主要降序排序

下一个示例演示如何在 LINQ 查询中使用 orderbydescending(在 Visual Basic 中为 Order By Descending)子句来按字符串的第一个字母对字符串进行降序排序。

Dim words() As String = {"the", "quick", "brown", "fox", "jumps"}

Dim sortQuery = From word In words _
                Order By word.Substring(0, 1) Descending _
                Select word

Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' the
' quick
' jumps
' fox
' brown

string[] words = { "the", "quick", "brown", "fox", "jumps" };

IEnumerable<string> query = from word in words
                            orderby word.Substring(0, 1) descending
                            select word;

foreach (string str in query)
    Console.WriteLine(str);

/* This code produces the following output:

    the
    quick
    jumps
    fox
    brown
*/

次要排序示例

次要升序排序

下面的示例演示如何在 LINQ 查询中使用 orderby(在 Visual Basic 中为 Order By)子句来对数组中的字符串执行主要和次要排序。首先按字符串长度,其次按字符串的第一个字母,对字符串进行升序排序。

Dim words() As String = {"the", "quick", "brown", "fox", "jumps"}

Dim sortQuery = From word In words _
                Order By word.Length, word.Substring(0, 1) _
                Select word

Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' fox
' the
' brown
' jumps
' quick

string[] words = { "the", "quick", "brown", "fox", "jumps" };

IEnumerable<string> query = from word in words
                            orderby word.Length, word.Substring(0, 1)
                            select word;

foreach (string str in query)
    Console.WriteLine(str);

/* This code produces the following output:

    fox
    the
    brown
    jumps
    quick
*/

次要降序排序

下一个示例演示如何在 LINQ 查询中使用 orderbydescending(在 Visual Basic 中为 Order By Descending)子句来按升序执行主要排序,按降序执行次要排序。首先按字符串长度,其次按字符串的第一个字母,对字符串进行排序。

Dim words() As String = {"the", "quick", "brown", "fox", "jumps"}

Dim sortQuery = From word In words _
                Order By word.Length, word.Substring(0, 1) Descending _
                Select word

Dim sb As New System.Text.StringBuilder()
For Each str As String In sortQuery
    sb.AppendLine(str)
Next

' Display the results.
MsgBox(sb.ToString())

' This code produces the following output:

' fox
' the
' quick
' jumps
' brown

string[] words = { "the", "quick", "brown", "fox", "jumps" };

IEnumerable<string> query = from word in words
                            orderby word.Length, word.Substring(0, 1) descending
                            select word;

foreach (string str in query)
    Console.WriteLine(str);

/* This code produces the following output:

    the
    fox
    quick
    jumps
    brown
*/

有关如何对数据进行排序的更多信息

Topic Location
orderby 子句(C# 参考) C# 程序员参考
如何:对 Join 子句的结果进行排序(C# 编程指南) C# 程序员参考
如何:按任意字或字段排序或筛选文本数据 (LINQ) 语言集成查询 (LINQ)
如何:使用 LINQ 对集合进行排序 (Visual Basic) Visual Basic 语言参考
orderby 子句(C# 参考) dv_csref
如何:对 Join 子句的结果进行排序(C# 编程指南) dv_csref
如何:按任意词或字段对文本数据进行排序或筛选 (LINQ) dv_Linq
如何:使用 LINQ 排序查询结果 (Visual Basic) dv_vbalr
orderby 子句(C# 参考) dv_csref
如何:对 Join 子句的结果进行排序(C# 编程指南) dv_csref
如何:按任意词或字段对文本数据进行排序或筛选 (LINQ) dv_Linq
如何:使用 LINQ 排序查询结果 (Visual Basic) dv_vbalr

请参见

概念

标准查询运算符概述

参考

orderby 子句(C# 参考)

Order By 子句 (Visual Basic)

System.Linq