共用方式為


排序資料

更新: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
*/

進一步了解如何排序資料

請參閱

概念

標準查詢運算子概觀

參考

orderby 子句 (C# 參考)

Order By 子句 (Visual Basic)

System.Linq