ThenByDescending<TSource,TKey> Method

System.Linq.Enumerable
Overview
Syntax
Parameters
Return Value
Exceptions
Examples

Overview

Sorts the elements of a sequence in descending order by using a specified key selector.

This method performs a stable sort. That is, the relative order of elements that have the same key is preserved.

A query that sorts the elements of a sequence in descending order by key.

Syntax

public static System.Collections.Generic.IEnumerable<TSource> ThenByDescending<TSource, TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
public static System.Collections.Generic.IEnumerable<TSource> ThenByDescending<TSource, TKey>(this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource, TKey> keySelector, System.Collections.Generic.IComparer<TKey> comparer)

Parameters

source
An IEnumerable<TSource> whose elements to order.
keySelector
A function to extract a TKey by which to sort each element.
comparer
An IComparer<TKey> to compare keys.

Return Value

An IOrderedEnumerable<TSource> whose elements are sorted in descending order by key.

Exceptions

ArgumentNullException
source is null.
-or-
keySelector is null.

Examples

Example 1: Sorting by Name in Descending Order

using System;
using System.Collections.Generic;
using System.Linq;

public class Product
{
    public string Name { get; set; }
    public int Price { get; set; }
}

public class Example
{
    public static void Main(string[] args)
    {
        var products = new List<Product>
        {
            new Product { Name = "Laptop", Price = 1200 },
            new Product { Name = "Mouse", Price = 25 },
            new Product { Name = "Keyboard", Price = 75 },
            new Product { Name = "Monitor", Price = 300 }
        };

        // Sort products by Price in descending order
        var sortedProducts = products.OrderBy(p => p.Name) // First sort by Name ascending
                                     .ThenByDescending(p => p.Price); // Then by Price descending

        foreach (var product in sortedProducts)
        {
            Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
        }
    }
}
Example 2: Sorting by Price Descending with a Custom Comparer

using System;
using System.Collections.Generic;
using System.Linq;

public class Product
{
    public string Name { get; set; }
    public int Price { get; set; }
}

public class CaseInsensitiveStringComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x == null && y == null) return 0;
        if (x == null) return -1;
        if (y == null) return 1;
        return string.Compare(x, y, StringComparison.OrdinalIgnoreCase);
    }
}

public class Example
{
    public static void Main(string[] args)
    {
        var products = new List<Product>
        {
            new Product { Name = "Laptop", Price = 1200 },
            new Product { Name = "mouse", Price = 25 },
            new Product { Name = "Keyboard", Price = 75 },
            new Product { Name = "Monitor", Price = 300 }
        };

        // Sort products by Price in descending order, then by Name case-insensitively descending
        var sortedProducts = products.OrderBy(p => p.Price)
                                     .ThenByDescending(p => p.Name, new CaseInsensitiveStringComparer());

        foreach (var product in sortedProducts)
        {
            Console.WriteLine($"Name: {product.Name}, Price: {product.Price}");
        }
    }
}