C# orderby 关键字
使用 orderby 关键字按升序或降序对列表进行排序。
以下是该列表 −
List<string> myList = new List<string>();
myList.Add("truck");
myList.Add("bus");
myList.Add("bicycle");
myList.Add("motorbike");现在让我们按降序对列表进行排序 −
myLen = from element in myList orderby element.Length descending select element;
以下是完整代码 −
示例
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<string> myList = new List<string>();
myList.Add("truck");
myList.Add("bus");
myList.Add("bicycle");
myList.Add("motorbike");
var myLen = from element in myList
orderby element.Length
select element;
Console.WriteLine("Ascending order...");
foreach (string str in myLen){
Console.WriteLine(str);
}
myLen = from element in myList
orderby element.Length descending
select element;
Console.WriteLine("Descending order...");
foreach (string str in myLen) {
Console.WriteLine(str);
}
}
}输出
Ascending order... bus truck bicycle motorbike Descending order... motorbike bicycle truck bus
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP