跳转至

排序运算⚓︎

一、排序运算方法⚓︎

在查询语法中,通过 orderby 对序列进行排序,第一个排序条件对元素执行主要排序,后续排序为次要排序,比如:

IEnumerable<Country> querySortedCountries =
    from country in countries
    // Area 主要排序,Population 次要排序
    orderby country.Area, country.Population descending 
    select country;

在方法语法中,主要排序和次要排序具有各自的方法可供调用,下表列出了对数据进行排序的标准查询运算符方法:

方法 说明 查询语法 连接
OrderBy 按升序对值排序 orderby Enumerable.OrderByQueryable.OrderBy
OrderByDescending 按降序对值排序 orderby ... descending Enumerable.OrderByDescendingQueryable.OrderByDescending
ThenBy 按升序执行次要排序 orderby …, … Enumerable.ThenByQueryable.ThenBy
ThenByDescending 按降序执行次要排序 orderby …, … descending Enumerable.ThenByDescendingQueryable.ThenByDescending
Reverse 反转集合中元素的顺序 × Enumerable.ReverseQueryable.Reverse

二、OrderBy⚓︎

OrderBy() 方法可以实现按值的升序排序,方法返回排序后的可迭代结果集。下面的代码展示了查询语法和方法语法按值升序排序的使用:

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

// 查询语法
IEnumerable<string> query1 = from word in words
                            orderby word.Length
                            select word;

foreach (string str in query1)
    Console.Write(str + " ");

Console.WriteLine();

// 方法语法
IEnumerable<string> query2 = words.OrderBy(word => word.Length);

foreach (string str in query2)
    Console.Write(str + " ");

/* This code produces the following output:  
    the fox quick brown jumps
    the fox quick brown jumps  
*/

三、OrderByDescending⚓︎

OrderByDescending() 方法可以实现按值的降序排序,方法返回排序后的可迭代结果集。下面的代码展示了查询语法和方法语法按值降序排序的使用:

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

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

foreach (string str in query1)
    Console.Write(str + " ");

Console.WriteLine();

IEnumerable<string> query2 = words.OrderByDescending(word => word.Length);

foreach (string str in query2)
    Console.Write(str + " ");

/* This code produces the following output:  
    quick brown jumps the fox
    quick brown jumps the fox
*/

四、ThenBy⚓︎

ThenBy() 方法实现次要排序升序,在查询语法中,会对非首个属性进行次要排序,在方法语法中通过该方法完成,次要排序默认升序。下面的代码展示了查询语法和方法语法对次要排序的使用:

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.Write(str + " ");

Console.WriteLine();

IEnumerable<string> query2 = words
    .OrderBy(word => word.Length)
    .ThenBy(word => word.Substring(0, 1));

foreach (string str in query2)
    Console.Write(str + " ");

/* This code produces the following output:  
    fox the brown jumps quick
    fox the brown jumps quick
*/

五、ThenByDescending⚓︎

ThenByDescending() 方法实现次要排序降序,在查询语法中,会对非首个属性进行次要排序,通过descending 关键字实现降序,在方法语法中通过该方法完成。下面的代码展示了查询语法和方法语法对次要排序降序的使用:

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.Write(str + " ");

Console.WriteLine();

IEnumerable<string> query2 = words
    .OrderBy(word => word.Length)
    .ThenByDescending(word => word.Substring(0, 1));

foreach (string str in query2)
    Console.Write(str + " ");

/* This code produces the following output:  
    the fox quick jumps brown
    the fox quick jumps brown
*/

六、Reverse⚓︎

Reverse() 方法可以实现对序列的反转。

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

foreach (string str in words)
    Console.Write(str + " ");

Console.WriteLine();

// 反转列表
IEnumerable<string> query = words.Reverse();

foreach (string str in query)
    Console.Write(str + " ");

/*
 This code produces the following output:
    the quick brown fox jumps
    jumps fox brown quick the
*/