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

在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();

 

posted @ 2019-06-16 09:34  江湖逍遥  阅读(11057)  评论(0编辑  收藏  举报