I need to sort an OrderedDictionary (System.Collections.Specialized)
I have this code:
var od = new System.Collections.Specialized.OrderedDictionary();
od.Add("a1", 3);
od.Add("a2", 5);
od.Add("a3", 2);
od.Add("a4", 4);
I wish to sort it using values. Can I do it using Linq?
Following will give you a sorted dictionary based on your OrderedDictionary.
There is one thing though,
ToDictionaryreturned a regular dictionary but the order is maintained in the dictionary for the lookup, as soon as any new item is inserted in the dictionary, they the order cannot be guaranteed. To avoid this, useSortedDictionary<TKey,TValue>which has a constructor that takes a regular dictionary as parameter(Make sure to replace
stringwith the correct types for Key and value in the above line).Output: