C# list.Orderby descending

后端 未结 6 1896
不知归路
不知归路 2020-11-30 22:05

I would like to receive a list sorted by \'Product.Name\' in descending order.

Similar to the function below which sorts the list

相关标签:
6条回答
  • 2020-11-30 22:28

    look it this piece of code from my project

    I'm trying to re-order the list based on a property inside my model,

     allEmployees = new List<Employee>(allEmployees.OrderByDescending(employee => employee.Name));
    

    but I faced a problem when a small and capital letters exist, so to solve it, I used the string comparer.

    allEmployees.OrderBy(employee => employee.Name,StringComparer.CurrentCultureIgnoreCase)
    
    0 讨论(0)
  • 2020-11-30 22:41
    list = new List<ProcedureTime>(); sortedList = list.OrderByDescending(ProcedureTime=> ProcedureTime.EndTime).ToList();
    

    Which works for me to show the time sorted in descending order.

    0 讨论(0)
  • 2020-11-30 22:47

    Yes. Use OrderByDescending instead of OrderBy.

    0 讨论(0)
  • 2020-11-30 22:49
    var newList = list.OrderBy(x => x.Product.Name).Reverse()
    

    This should do the job.

    0 讨论(0)
  • 2020-11-30 22:55

    Sure:

    var newList = list.OrderByDescending(x => x.Product.Name).ToList();
    

    Doc: OrderByDescending(IEnumerable, Func).

    In response to your comment:

    var newList = list.OrderByDescending(x => x.Product.Name)
                      .ThenBy(x => x.Product.Price)
                      .ToList();
    
    0 讨论(0)
  • 2020-11-30 22:55
    list.OrderByDescending();
    

    works for me.

    0 讨论(0)
提交回复
热议问题