ThenByDescending
Just like the ThenBy operation, you can order on multiple properties in descending order by using the ThenByDescending
.
Basic example
We have an anonymous object that represents a person. If we want to order these by name in descending order, then we want to order them by age in descending order, we can do the following:
Method example
Live example: https://dotnetfiddle.net/HOCYqk
var people = new[]
{
new {Name = "Jennifer", Age = 21},
new {Name = "Alton", Age = 22},
new {Name = "Adam", Age = 27},
new {Name = "Milford", Age = 23},
new {Name = "James", Age = 23}
};
var orderedPeople = people.OrderByDescending(x => x.Name)
.ThenByDescending(x => x.Age);
// output:
// { Name = Milford, Age = 23 }
// { Name = Jennifer, Age = 21 }
// { Name = James, Age = 23 }
// { Name = Alton, Age = 22 }
// { Name = Adam, Age = 27 }
Query syntax
Live example: https://dotnetfiddle.net/Y2iSKq
var people = new[]
{
new {Name = "Jennifer", Age = 21},
new {Name = "Alton", Age = 22},
new {Name = "Adam", Age = 27},
new {Name = "Milford", Age = 23},
new {Name = "James", Age = 23}
};
var orderedPeople = from person in people
orderby person.Name descending, person.Age descending
select person;
// output:
// { Name = Milford, Age = 23 }
// { Name = Jennifer, Age = 21 }
// { Name = James, Age = 23 }
// { Name = Alton, Age = 22 }
// { Name = Adam, Age = 27 }