TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Comparison Delegate

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

Comparison. The Comparison type enables custom sorting.

We implement Comparison(T) using its constructor. The target is a method that receives two parameters of the appropriate type and returns an int. It determines sorting order of the parameters.

Generic Class

Example. This program introduces two static methods. In the Main entry point control flow begins. And the CompareLength method receives two strings and returns an int. In the Main entry point, we create an array of strings and then display it.

String Array

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.

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);
	}
    }
}

Output

bird
elephant
dog
cat
mouse

cat
dog
bird
mouse
elephant

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.

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 CompareTo with the lengths of both strings.

CompareTo Int Method

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

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. Also it can be used with the List.Sort or Array.Sort methods.

Sort KeyValuePair ListSort TupleSort List MethodArray.Sort Examples

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.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf