0
0
CsharpHow-ToBeginner · 4 min read

How to Use Custom Comparer in C# for Custom Sorting and Equality

In C#, you use a custom comparer by implementing the IComparer<T> interface for sorting or IEqualityComparer<T> for equality checks. Then, pass your custom comparer instance to methods like List<T>.Sort() or collections like Dictionary<TKey, TValue> to control how objects are compared.
📐

Syntax

To create a custom comparer, implement the IComparer<T> interface for sorting or IEqualityComparer<T> for equality. Your class must define the Compare method (for sorting) or Equals and GetHashCode methods (for equality).

Example syntax for sorting:

public class MyComparer : IComparer<T> {
    public int Compare(T x, T y) {
        // return -1 if x < y, 0 if equal, 1 if x > y
    }
}
csharp
public class MyComparer : IComparer<string>
{
    public int Compare(string x, string y)
    {
        // Custom comparison logic here
        return x.Length.CompareTo(y.Length);
    }
}
💻

Example

This example shows how to sort a list of strings by their length using a custom comparer.

csharp
using System;
using System.Collections.Generic;

public class LengthComparer : 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 x.Length.CompareTo(y.Length);
    }
}

public class Program
{
    public static void Main()
    {
        List<string> fruits = new List<string> { "apple", "banana", "fig", "cherry" };
        fruits.Sort(new LengthComparer());
        foreach (var fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}
Output
fig apple banana cherry
⚠️

Common Pitfalls

  • Not handling null values in Compare or Equals methods can cause exceptions.
  • For equality comparers, forgetting to override GetHashCode consistently with Equals breaks hash-based collections.
  • Returning incorrect values from Compare (anything other than negative, zero, or positive) can cause unexpected sorting behavior.
  • Using reference equality instead of value equality when comparing objects.
csharp
/* Wrong way: ignoring nulls and inconsistent GetHashCode */
public class BadComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y) => object.ReferenceEquals(x, y); // Reference equality
    public int GetHashCode(string obj) => obj.GetHashCode();
}

/* Right way: handle nulls and use value equality */
public class GoodComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        if (x == null && y == null) return true;
        if (x == null || y == null) return false;
        return x.Equals(y);
    }
    public int GetHashCode(string obj) => obj?.GetHashCode() ?? 0;
}
📊

Quick Reference

InterfacePurposeKey Method(s)
IComparerDefines custom sorting orderint Compare(T x, T y)
IEqualityComparerDefines custom equality for hash collectionsbool Equals(T x, T y), int GetHashCode(T obj)

Key Takeaways

Implement IComparer to define custom sorting logic with the Compare method.
Implement IEqualityComparer to define custom equality and hashing for collections like Dictionary.
Always handle null values safely in your comparer methods to avoid exceptions.
Ensure GetHashCode is consistent with Equals when implementing IEqualityComparer.
Pass your custom comparer instance to sorting methods or collection constructors to use it.