时间:2021-05-20
复制代码 代码如下:
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; }
}
}
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#对list列表进行随机排序的方法。分享给大家供大家参考。具体实现方法如下:publicListRandomSortList(ListListT
本文实例讲述了C#选择排序法。分享给大家供大家参考。具体如下:publicstaticvoidSelectSort(int[]list){for(inti=0;
本文实例讲述了C#插入法排序算法。分享给大家供大家参考。具体如下:publicstaticvoidInsertSort(int[]list){for(inti=
本文实例讲述了C#堆排序实现方法。分享给大家供大家参考。具体如下:privatestaticvoidAdjust(int[]list,inti,intm){in
本文实例讲述了C#使用IComparer自定义List类实现排序的方法。分享给大家供大家参考。具体如下:List类中不带参数的Sort函数可以用来为List类中