C# list.Orderby descending

C#ListSortingSql Order-By

C# Problem Overview


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

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

Yes. Use OrderByDescending instead of OrderBy.

Solution 3 - C#

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

This should do the job.

Solution 4 - C#

list.OrderByDescending();

works for me.

Solution 5 - C#

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)

Solution 6 - C#

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

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPFranchiseView Question on Stackoverflow
Solution 1 - C#StriplingWarriorView Answer on Stackoverflow
Solution 2 - C#Mark ByersView Answer on Stackoverflow
Solution 3 - C#BeedjeesView Answer on Stackoverflow
Solution 4 - C#TabletView Answer on Stackoverflow
Solution 5 - C#Basheer AL-MOMANIView Answer on Stackoverflow
Solution 6 - C#ArchanaView Answer on Stackoverflow