欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#对list排序示例

程序员文章站 2024-02-18 08:04:34
复制代码 代码如下: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; }
}
}