排序資料
排序作業會根據一個或多個屬性來排序序列中的項目。 第一個排序準則會對項目執行主要排序。 而透過指定第二個排序準則,可以排序每個主要排序群組內的項目。
下圖顯示對字元序列執行字母排序作業的結果。
下節會列出排序資料的標準查詢運算子方法。
方法
方法名稱 |
描述 |
C# 查詢運算式語法 |
Visual Basic 查詢運算式語法 |
詳細資訊 |
---|---|---|---|---|
OrderBy |
將值依遞增順序排序。 |
orderby |
Order By |
|
OrderByDescending |
將值依遞減順序排序。 |
orderby … descending |
Order By … Descending |
|
ThenBy |
以遞增順序執行次要排序。 |
orderby …, … |
Order By …, … |
|
ThenByDescending |
以遞減順序執行次要排序。 |
orderby …, … descending |
Order By …, … Descending |
|
Reverse |
反轉集合中項目的順序。 |
不適用。 |
不適用。 |
查詢運算式語法範例
主要排序範例
主要遞增排序
下列範例示範如何在 LINQ 查詢中使用 orderby (在 Visual Basic 中是 Order By) 子句,將陣列中的字串依長度遞增排序。
Dim words = {"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 查詢中使用 orderby descending (在 Visual Basic 中是 Order By Descending) 子句,將字串依第一個字母遞減排序。
Dim words = {"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 = {"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 查詢中使用 orderby descending (在 Visual Basic 中是 Order By Descending) 子句,先依遞增順序做主要排序,再依遞減順序做次要排序。 字串會先依長度做主要排序,然後再依第一個字母做次要排序。
Dim words = {"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
*/
請參閱
工作
HOW TO:排序 Join 子句的結果 (C# 程式設計手冊)
HOW TO:使用 LINQ 排序查詢結果 (Visual Basic)
HOW TO:依任何字或欄位排序或篩選文字資料 (LINQ)