["","","37 C# ","A*erwttz3BZCCST~~}T~~YF66884G5G75G5F+CXBCZBBBBXS}T~~}T~~","Comparison."," This type enables custom sorting. 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 ","generic","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. In Main, we create an array of strings and then display it. ","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.","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.7 (2017)\n\n","C# program that uses Comparison delegate","\n\nusing System;\n\nclass Program\n{\n static int CompareLength(string a, string b)\n {","\n // Return result of CompareTo with lengths of both strings.\n ","return a.Length.CompareTo(b.Length);\n }\n\n static void Main()\n {","\n // Source array.\n ","string[] array =\n {\n ","\"bird\"",",\n ","\"elephant\"",",\n ","\"dog\"",",\n ","\"cat\"",",\n ","\"mouse\"","\n };","\n\n // Display array.\n ","foreach"," (string element in array)\n {\n Console.WriteLine(element);\n }\n Console.WriteLine();","\n\n // Create Comparison instance and use it.\n ","Comparison","<string> comparison = new Comparison<string>(CompareLength);\n ","Array.Sort","(array, comparison);","\n\n // Display array.\n ","foreach"," (string element in array)\n {\n Console.WriteLine(element);\n }\n }\n}\n\n","Output","\n\nbird\nelephant\ndog\ncat\nmouse\n\ncat\ndog\nbird\nmouse\nelephant","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 ","compareto","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 List ","sort-keyvaluepair","Sort Tuple ","tuple","Sort List Method ","sort-list","Array.Sort Examples ","array-sort","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. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]