C#orderby关键字
使用orderbyleyword对列表进行升序或降序排序。
以下是列表-
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
热门推荐
5 天仓节祝福语简短
10 霸气新年祝福语大全简短
11 公司生日祝福语员工简短
12 关于旅游的简短祝福语
13 预祝中秋简短的祝福语
14 中午祝福语简短句子
15 奶奶的生日简短祝福语
16 客户去旅游祝福语简短
17 手写妈妈生日祝福语简短
18 小朋友上学祝福语简短