C# Comparison Delegate

Generic type

The Comparison type enables custom sorting. We implement Comparison<T> using the Comparison constructor. The target is a method that receives two parameters of the appropriate type and returns an int. It contains custom logic to determine sorting order of the parameters.

This C# example program uses the Comparison delegate type with the Array.Sort method.

Example

Main method

This program introduces two static methods: the CompareLength method, which receives two strings and returns an int; and the Main entry point. In the Main entry point, we create an array of strings and then display it. Then, we create a new Comparison<string> using the CompareLength method as the target. Finally, we sort the array again using this Comparison delegate and display it.

Program that uses Comparison delegate [C#]

using System;

class Program
{
    static int CompareLength(string a, string b)
    {
	// Return result of CompareTo with lengths of both strings.
	return a.Length.CompareTo(b.Length);
    }

    static void Main()
    {
	// Source array.
	string[] array =
	{
	    "bird",
	    "elephant",
	    "dog",
	    "cat",
	    "mouse"
	};

	// Display array.
	foreach (string element in array)
	{
	    Console.WriteLine(element);
	}
	Console.WriteLine();

	// Create Comparison instance and use it.
	Comparison<string> comparison = new Comparison<string>(CompareLength);
	Array.Sort(array, comparison);

	// Display array.
	foreach (string element in array)
	{
	    Console.WriteLine(element);
	}
    }
}

Output

bird
elephant
dog
cat
mouse

cat
dog
bird
mouse
elephant
This section provides information

Requirements. The Comparison<T> type requires that you specify a method that receives two parameters of type T and then returns an int. You pass the method name to the constructor. You can often implicitly use the Comparison delegate by just passing the target method name.

Comparison target method. So how do you implement the code inside the target method (CompareLength)? You typically use the CompareTo method on a property, field, or other information derived from each type. Here, we use the CompareTo method with the lengths of both strings. To reverse the sort, just reverse the order of a and b in the CompareTo expression.

CompareTo Int Method

Examples

Note (please read)

There are many other examples of using the Comparison delegate type on this site. You can use Comparison without specifying the constructor explicitly; you can use it with the lambda expression syntax. Also it can be used with the List.Sort or Array.Sort methods.

Sort KeyValuePair List Sort Tuple List Sort List Method Array.Sort Examples

Summary

The C# programming language

We looked at the Comparison type. We provided an example of the Comparison<T> constructor, but also pointed out that it is not necessary to provide in all cases. We implemented the target method of the Comparison delegate by using the CompareTo method. Finally, we used the Comparison delegate type with the Array.Sort method.

Delegate Tutorial
.NET