Csharp/C#教程:c#对list排序示例分享

代码如下:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Text;
namespaceListSort
{
classProgram
{
staticvoidMain(string[]args)
{
ListlistCustomer=newList();
listCustomer.Add(newCustomer{name=”客户1″,id=0});
listCustomer.Add(newCustomer{name=”客户2″,id=1});
listCustomer.Add(newCustomer{name=”客户3″,id=5});
listCustomer.Add(newCustomer{name=”客户4″,id=3});
listCustomer.Add(newCustomer{name=”客户5″,id=4});
listCustomer.Add(newCustomer{name=”客户6″,id=5});
///升序
ListlistCustomer1=listCustomer.OrderBy(s=>s.id).ToList();
//降序
ListlistCustomer2=listCustomer.OrderByDescending(s=>s.id).ToList();
//Linq排序方式
ListlistCustomer3=(fromcinlistCustomer
orderbyc.iddescending//ascending
selectc).ToList();
Console.WriteLine(“List.OrderBy方法升序排序”);
foreach(CustomercustomerinlistCustomer1)
{
Console.WriteLine(customer.name);
}
Console.WriteLine(“List.OrderByDescending方法降序排序”);
foreach(CustomercustomerinlistCustomer2)
{
Console.WriteLine(customer.name);
}
Console.WriteLine(“Linq方法降序排序”);
foreach(CustomercustomerinlistCustomer3)
{
Console.WriteLine(customer.name);
}
Console.ReadKey();
}
}
classCustomer
{
publicintid{get;set;}
publicstringname{get;set;}
}
}

上述就是C#学习教程:c#对list排序示例分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)! 您可能感兴趣的文章:关于C#泛型列表List<T>的基本用法

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/905164.html

(0)
上一篇 2021年10月22日
下一篇 2021年10月22日

精彩推荐