首页 > 编程语言 > 详细

【转载】 C#中List集合使用OrderByDescending方法对集合进行倒序排序

时间:2019-06-16 10:30:45      阅读:187      评论:0      收藏:0      [点我收藏+]

在C#的List集合操作中,有时候需要针对List集合进行排序操作,如果是对List集合按照元素对象或者元素对象的某个属性进行倒序排序的话,可以使用OrderByDescending方法来实现,OrderByDescending方法属于List集合的扩展方法,方法的调用形式为使用Lambda表达式语句。

(1)对List<int>集合对象list1进行从大到小降序排序可使用下列语句:

 List<int> list1 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 list1 = list1.OrderByDescending(t => t).ToList();

(2)按List集合中的元素对象的某个具体属性进行倒序排序也可使用OrderByDescending方法。

我们需要对List<TestModel>集合对象testList进行排序,排序规则为按照对象属性Index降序排序。

首先看下TestModel的定义:

    public class TestModel
    {
         public int Index { set; get; }

        public string Name { set; get; }
    }

对testList集合按元素对象的Index属性进行倒序排序可使用下列语句:

List<TestModel> testList = new List<ConsoleApplication1.TestModel>();
testList = testList.OrderByDescending(t => t.Index).ToList();

备注:原文转载自博主个人站IT技术小趣屋,原文链接C#中List集合使用OrderByDescending方法对集合进行倒序排序_IT技术小趣屋

【转载】 C#中List集合使用OrderByDescending方法对集合进行倒序排序

原文:https://www.cnblogs.com/xu-yi/p/11029660.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!