开发者

C# list.Orderby descending

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

Similar to the function below which sorts the list in ascending order, 开发者_如何学Pythonjust in reverse, is this possible?

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


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


Yes. Use OrderByDescending instead of OrderBy.


var newList = list.OrderBy(x => x.Product.Name).Reverse()

This should do the job.


list.OrderByDescending();

works for me.


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)


list = new List<ProcedureTime>(); sortedList = list.OrderByDescending(ProcedureTime=> ProcedureTime.EndTime).ToList();

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜