OrderByDescending
When you need to order in adescending order, you can use the OrderByDescending
method.
Basic example
Here we will be ordering a set of cities alphabetically in descending order.
Method syntax
As you can see, using the OrderByDescending
the cities are now ordered from Z to A.
Live example: https://dotnetfiddle.net/MQKGNF
var cities = new[] { "Barcelona", "London", "Paris", "New York", "Moscow", "Amsterdam", "Tokyo", "Florence" };
var orderedCities = cities.OrderByDescending(s => s);
// output: Tokyo, Paris, New York, Moscow, London, Florence, Barcelona, Amsterdam
Query syntax
Similar to the OrderBy query syntax, the descending
keyword is needed to order by descending.
Live example: https://dotnetfiddle.net/lF3OKk
var cities = new[] { "Barcelona", "London", "Paris", "New York", "Moscow", "Amsterdam", "Tokyo", "Florence" };
var orderedCities = from city in cities
orderby city descending
select city;
// output: Tokyo, Paris, New York, Moscow, London, Florence, Barcelona, Amsterdam