C# list.Orderby descending

PFranchise picture PFranchise · Oct 13, 2010 · Viewed 344.6k times · Source

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, just in reverse, is this possible?

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

Answer

StriplingWarrior picture StriplingWarrior · Oct 13, 2010

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