If you want to sort a dictionary by value? Use the following c# code:
1 2 3 4 5 6 7 8 9 10 11 | using System.Linq.Enumerable; ... List<KeyValuePair<string, string>> myList = aDictionary.ToList(); myList.Sort( delegate(KeyValuePair<string, string> pair1, KeyValuePair<string, string> pair2) { return pair1.Value.CompareTo(pair2.Value); } ); |
Since you’re targeting .NET 2.0 or above, you can simplify this into lambda syntax — it’s equivalent, but shorter. If you’re targeting .NET 2.0 you can only use this syntax if you’re using the compiler from Visual Studio 2008 (or above).
1 2 3 | var myList = aDictionary.ToList(); myList.Sort((pair1,pair2) => pair1.Value.CompareTo(pair2.Value)); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.