180

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();
2
  • 1
    I think he meant that it doesn't accept the descending keyword, because he didn't begin his expression with from x in list... Oct 13, 2010 at 15:30
  • 1
    Sorry, I did not copy that code exactly, but typed it from memory. My actual code works, but just returns a list that is sorted in ascending order.
    – PFranchise
    Oct 13, 2010 at 15:31

7 Answers 7

314

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();
2
  • 2
    So your edit will sort by name(from z->a) then price (low -> high)?
    – PFranchise
    Oct 13, 2010 at 15:29
  • 14
    Yes, that is correct. Calls to OrderBy or ThenBy are always ascending. The OrderByDescending and ThenByDescending methods are what you'd use for descending. Oct 13, 2010 at 15:33
26

Yes. Use OrderByDescending instead of OrderBy.

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

This should do the job.

13
list.OrderByDescending();

works for me.

1
  • 4
    This did nothing without doing list = list.OrderByDescending().toList();
    – Almo
    Aug 4, 2014 at 14:19
4

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)
1

2 options for sorting a List in decending order

Method 1 Order against a specified key using OrderByDescending()

List<Product> myList = new() {
    new Product("A"),
    new Product("Z"),
    new Product("C")
};
myList = myList.OrderByDescending(x => x.Name).ToList();
Debug.WriteLine(
    $"{myList[0].Name}" + // 'Z'
    $"{myList[1].Name}" + // 'C'
    $"{myList[2].Name}"   // 'A'
);

Example Product

class Product
{
    public string Name { get; private set; }
    public Product(string Name)
    {
        this.Name = Name;
    }
}

Method 2 Sort ascending and use Reverse()

myList = myList.OrderBy(x => x.Name).ToList();
myList.Reverse();

Reverse() is convenient for generic types such as List<string>

Reverse sort a string list

List<string> stringList= new() { "A", "Z", "C" };
stringList.Sort();
stringList.Reverse();
-2
list = new List<ProcedureTime>(); sortedList = list.OrderByDescending(ProcedureTime=> ProcedureTime.EndTime).ToList();

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

0

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.