How can I sort generic list DESC and ASC?

前端 未结 5 2106
盖世英雄少女心
盖世英雄少女心 2020-12-04 14:07

How can I sort generic list DESC and ASC? With LINQ and without LINQ? I\'m using VS2008.

class Program
{
    static void Main(string[] args)
    {
        Li         


        
5条回答
  •  暖寄归人
    2020-12-04 14:18

    I was checking all the answer above and wanted to add one more additional information. I wanted to sort the list in DESC order and I was searching for the solution which is faster for bigger inputs and I was using this method earlier :-

    li.Sort();
    li.Reverse();
    

    but my test cases were failing for exceeding time limits, so below solution worked for me:-

    li.Sort((a, b) => b.CompareTo(a));
    

    So Ultimately the conclusion is that 2nd way of Sorting list in Descending order is bit faster than the previous one.

提交回复
热议问题