C# Comparison Object, Used With Array.SortUse the Comparison delegate type with the Array.Sort method. See static method and lambda syntax.
dot net perls

Comparison. This type enables custom sorting. It is often used with Array.Sort or List.Sort. We implement Comparison(T) using its constructor.


The target is a method that receives 2 parameters of the appropriate type and returns an int. It determines sorting order of the parameters.

Generic Class, Method

An example. This program introduces 2 static methods. In the Main entry point control flow begins. And the CompareLength method receives 2 strings and returns an int.

Here We create an array of strings. Then we create a new Comparison<string> object using the CompareLength method as the target.

Finally We sort the array again using this Comparison delegate and display it.

CompareLength We use CompareTo with the lengths of both strings. CompareTo returns an integer value indicating the comparison result.


Reverse To reverse the sort, just reverse the order of the CompareTo expression variables.

C# program that uses Comparison delegate
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); } } }
bird elephant dog cat mouse cat dog bird mouse elephant

Lambda example. We do not need to use a static method for the Comparison implementation. We can instead use a lambda expression. Here we sort on string lengths with a lambda Comparison.


C# program that uses Comparison lambda
using System; class Program { static void Main() { string[] array = { "4444", "1", "22", "333" }; // Use lambda to sort on length. Array.Sort(array, (a, b) => a.Length.CompareTo(b.Length)); // Write result. Console.WriteLine("RESULT: {0}", string.Join(";", array)); } }
RESULT: 1;22;333;4444

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.

Tip You can often implicitly use the Comparison delegate by just passing the target method name.

Notes, target method. How do you implement the code inside the target method (like CompareLength)? Use the CompareTo method on a property, field, or other value from each type.

Discussion. There are many other examples of using Comparison. You can use Comparison without specifying the constructor explicitly. It is compatible with the lambda expression syntax.

Sort KeyValuePair List


Also A Comparison can be used with the List.Sort or Array.Sort methods. Try caching the lambda for a small speedup.

Sort List


A summary. 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 by using the CompareTo method. CompareTo is not required, but it fills the comparison return value requirement easily.

© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.