c#对list排序示例
更新时间:2014年01月06日 11:22:27 作者:
本文主要介绍了c#对List成员排序的示例,大家参考使用吧
复制代码 代码如下:
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; }
}
}
相关文章
C#中的Task.Delay()和Thread.Sleep()区别(代码案例)
Task.Delay(),async/await和CancellationTokenSource组合起来使用可以实现可控制的异步延迟。本文通过多种代码案例给大家分析C#中的Task.Delay()和Thread.Sleep()知识,感兴趣的朋友一起看看吧2021-06-06
最新评论