当前位置: 移动技术网 > IT编程>开发语言>c# > c#对list排序示例

c#对list排序示例

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:using system; using system.collections.generic; using system.linq; using sys

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.text;
namespace listsort
{
class program
{
static void main(string[] args)
{
list listcustomer = new list();
listcustomer.add(new customer { name = "客户1", id = 0 });
listcustomer.add(new customer { name = "客户2", id = 1 });
listcustomer.add(new customer { name = "客户3", id = 5 });
listcustomer.add(new customer { name = "客户4", id = 3 });
listcustomer.add(new customer { name = "客户5", id = 4 });
listcustomer.add(new customer { name = "客户6", id = 5 });
///升序
list listcustomer1 = listcustomer.orderby(s => s.id).tolist();
//降序
list listcustomer2 = listcustomer.orderbydescending(s => s.id).tolist();
//linq排序方式
list listcustomer3 = (from c in listcustomer
orderby c.id descending //ascending
select c).tolist();
console.writeline("list.orderby方法升序排序");
foreach (customer customer in listcustomer1)
{
console.writeline(customer.name);
}
console.writeline("list.orderbydescending方法降序排序");
foreach (customer customer in listcustomer2)
{
console.writeline(customer.name);
}
console.writeline("linq方法降序排序");
foreach (customer customer in listcustomer3)
{
console.writeline(customer.name);
}
console.readkey();
}
}
class customer
{
public int id { get; set; }
public string name { get; set; }
}
}

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网