How to declare natural ordering by implementing the generic IComparer interface in C# .NET

In this post we showed how to declare natural ordering for a custom type by implementing the generic IComparable interface. We saw that it required us to implement the CompareTo method. The example we looked at was a simple Triangle class where we said that triangles can be ordered based on their areas. That’s probably a reasonable comparison for triangle.

However, what about the following object?

public class Building
{
	public double Area { get; set; }
	public int NumberOfRooms { get; set; }
	public string Address { get; set; }
	public bool ForSale { get; set; }
	public DateTime DateBuilt { get; set; }
}

We can order buildings by pretty much any of these properties. If we implement the IComparable interface then we have to choose which one we use for ordering purposes. That’s not very flexible. You might want to be able to choose your sorting logic during execution time based on the user’s preferences.

That’s where comparers enter the scene. The generic IComparer interface allows you to create custom comparers and use them where sorting is needed. It has a single method called Compare which returns an integer. The returned integer follows the same rule as the integer returned by the IComparable.CompareTo method.

Here come a couple of examples:

public class BuildingDateBuiltComparer : IComparer<Building>
{
	public int Compare(Building x, Building y)
	{
		return x.DateBuilt.CompareTo(y.DateBuilt);
	}
}

public class BuildingAddressComparer : IComparer<Building>
{
	public int Compare(Building x, Building y)
	{
		return x.Address.CompareTo(y.Address);
	}
}

public class BuildingAreaComparer : IComparer<Building>
{
	public int Compare(Building x, Building y)
	{
		return x.Area.CompareTo(y.Area);
	}
}

You can use your custom comparers in any method that accepts an IComparer object. Let’s first have a list of buildings:

Building b1 = new Building()
{
	Address = "London",
	Area = 123,
	DateBuilt = DateTime.UtcNow.AddYears(-5),
	ForSale = true,
	NumberOfRooms = 5
};
Building b2 = new Building()
{
	Address = "New York",
	Area = 100,
	DateBuilt = DateTime.UtcNow.AddYears(-8),
	ForSale = false,
	NumberOfRooms = 4
};
Building b3 = new Building()
{
	Address = "Amsterdam",
	Area = 114,
	DateBuilt = DateTime.UtcNow.AddYears(-2),
	ForSale = true,
	NumberOfRooms = 3
};
Building b4 = new Building()
{
	Address = "Stockholm",
	Area = 167,
	DateBuilt = DateTime.UtcNow.AddYears(-1),
	ForSale = false,
	NumberOfRooms = 6
};
Building b5 = new Building()
{
	Address = "Budapest",
	Area = 120,
	DateBuilt = DateTime.UtcNow.AddYears(-25),
	ForSale = true,
	NumberOfRooms = 4
};
List<Building> buildings = new List<Building>();
buildings.Add(b1);
buildings.Add(b2);
buildings.Add(b3);
buildings.Add(b4);
buildings.Add(b5);

Say you’d like to sort this list by the construction date:

buildings.Sort(new BuildingDateBuiltComparer());

Sort by area? Piece of cake:

buildings.Sort(new BuildingAreaComparer());

There’s also an abstract base class called Comparer of T which in turn implements the generic IComparer of T and the non-generic IComparer interface. If you’d like to make your comparers available to old .NET clients which don’t know generics then you can extend the base Comparer class instead with minimal change. Here’s an example:

public class BuildingAreaComparer : Comparer<Building>
{
	public override int Compare(Building x, Building y)
	{
		return x.Area.CompareTo(y.Area);
	}
}

There’s no much difference. The “I” is gone from “IComparer” and we are overriding the Compare method of Comparer so we need to add the override keyword to the method declaration.

View all various C# language feature related posts here.

Advertisement

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

One Response to How to declare natural ordering by implementing the generic IComparer interface in C# .NET

  1. Imran Yaseen says:

    Awesome work!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: