C# List排序简介及四种方法介绍-附代码

有时用户需要按某项排序,但是查询结果以List格式存储,我们当然可以自己编写一个快速排序的方法进行排序,但是还有多个选择,并且可能比你写的短、效率也不差,那不如在恰当的时候选择其他方法对List进行排序。

1,对于普通的int,double型列表List,如下:

List<int> list = new List<int>();
list.Sort(); // 升序排序
list.Reverse(); // 降序排序:先升序排序,再反转顺序 

2,对于Class型的列表List,根据类的某一属性进行排序,如下:

方法一: List的OrderBy与OrderByDescending方法

list.OrderBy(a => a.Value).ToList();//升序
list.OrderByDescending(a => a.Value).ToList();//降序

方法二:使用Sort方法,

list.Sort((a, b) => a.Value.CompareTo(b.Value));//升序
list.Sort((a, b) => b.Value.CompareTo(a.Value));//降序

方法三:使用委托

list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });

方法四:使用Linq

c#的一大特色就是Linq,他遍布c#里里外外,利用他可以减少大量的代码,不过他是从SQL中出来的,所以SQL还是了解之后,这个就好懂了。具体代码如下所示:

List<CapVal> upList = (from cv in list
                       orderby cv.Value ascending
                       select cv).ToList();//升序
List<CapVal> downList = (from cv in list
                         orderby cv.Value descending
                         select cv).ToList();//降序
public class ListTest
{
    public static void SortList()
    {
        List<CapVal> list = new List<CapVal>();
        list.Add(new CapVal("一月", 3));
        list.Add(new CapVal("二月", 21));
        list.Add(new CapVal("三月", 12));
        list.Add(new CapVal("四月", 6));
        list.Add(new CapVal("五月", 9));
        list.Add(new CapVal("六月", 3));
        list.Add(new CapVal("七月", 17));
        list.Add(new CapVal("八月", 29));
        list.Add(new CapVal("九月", 19));
        Console.WriteLine("排序前——————" );
        foreach(CapVal item in list)
        {
            Console.WriteLine(item.Caption + ":" + item.Value);
        }
        Console.WriteLine("升序排序序后———OrderBy———");
        List<CapVal> upList = list.OrderBy(a => a.Value).ToList();
         Console.WriteLine("升序排序序后———Sort———");
        list.Sort((a, b) => a.Value.CompareTo(b.Value));
        List<CapVal> upList = list;
        Console.WriteLine("升序排序序后———使用委托———");
        list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv1.Value.CompareTo(cv2.Value); });
        List<CapVal> upList = list;
        Console.WriteLine("升序排序序后———使用Linq———");
        List<CapVal> upList = (from cv in list
                       orderby cv.Value ascending
                       select cv).ToList();//升序
        foreach (CapVal item in upList)
        {
            Console.WriteLine(item.Caption + ":" + item.Value);
        }
        Console.WriteLine("降序排序序后———OrderBy———");
        List<CapVal> downList = list.OrderByDescending(a => a.Value).ToList();
        Console.WriteLine("降序排序序后———Sort———");
        list.Sort((a, b) => b.Value.CompareTo(a.Value));
        List<CapVal> downList = list;
        Console.WriteLine("降序排序序后———使用委托———");
        list.Sort(delegate (CapVal cv1, CapVal cv2) { return cv2.Value.CompareTo(cv1.Value); });
        List<CapVal> downList = list;
        Console.WriteLine("降序排序序后———使用Linq———");
        List<CapVal> downList = (from cv in list
                         orderby cv.Value descending
                         select cv).ToList();//降序
        List<CapVal> downList = list;
        foreach (CapVal item in downList)
        {
            Console.WriteLine(item.Caption + ":" + item.Value);
        }
        Console.ReadKey();
    }
}
public class CapVal
{
    public string Caption { get; set; }
    public decimal Value { get; set; }

    public CapVal(String _caption, decimal _value)
    {
        this.Caption = _caption;
        this.Value = _value;
    }
}

  • 13
    点赞
  • 61
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值