TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# Sort KeyValuePair List: CompareTo

Sort KeyValuePair structs in a List with CompareTo. Use a Comparison delegate.
Sort KeyValuePair List. A List contains many KeyValuePair struct instances. It can be sorted with the Sort List method. This requires Comparison delegate implementations. We implement these as static int methods that call CompareTo.ComparisonCompareToSort
This example code introduces 3 static methods. Compare1 receives two KeyValuePair structs and returns the result of CompareTo on their Key. Compare2 returns the CompareTo of the Value. And in the Main entry point control flow begins.KeyValuePair

Main: A new List of KeyValuePair instances is created. Three KeyValuePairs are created and added to the List.

Next: The List is sorted with Compare1. The method target Compare1 is accepted as a Comparison delegate.

Then: After printing the List, we sort it again with Compare2, and finally display the List again.

ListConsole
C# program that sorts List of KeyValuePair instances using System; using System.Collections.Generic; class Program { static int Compare1(KeyValuePair<string, int> a, KeyValuePair<string, int> b) { return a.Key.CompareTo(b.Key); } static int Compare2(KeyValuePair<string, int> a, KeyValuePair<string, int> b) { return a.Value.CompareTo(b.Value); } static void Main() { var list = new List<KeyValuePair<string, int>>(); list.Add(new KeyValuePair<string, int>("Perl", 7)); list.Add(new KeyValuePair<string, int>("Net", 9)); list.Add(new KeyValuePair<string, int>("Dot", 8)); // Use Compare1 as comparison delegate. list.Sort(Compare1); foreach (var pair in list) { Console.WriteLine(pair); } Console.WriteLine(); // Use Compare2 as comparison delegate. list.Sort(Compare2); foreach (var pair in list) { Console.WriteLine(pair); } } } Output [Dot, 8] [Net, 9] [Perl, 7] [Perl, 7] [Dot, 8] [Net, 9]
The results reveal the effects of the Comparison delegates. The List.Sort(Compare1) call sorts the List by the Key of the KeyValuePair. And the List.Sort(Compare2) call sorts the List by the Value int.Sort List
Summary. With this example program, we implemented one way of sorting collections of KeyValuePair instances. You can use other syntax forms and implementations, such as LINQ expressions or lambda expressions, to accomplish this task as well.orderbyLambda
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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