c# list排序 c#对list排序示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

c# list排序 c#对list排序示例

  2021-03-19 我要评论
想了解c#对list排序示例的相关内容吗,在本文为您仔细讲解c# list排序的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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; }
}
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们