I'd like to order a dictionary by the value. It seems the way it would usually be done in .Net doesn't exist in the Mono API.
Is there any particular API call or should I do this myself?
Thanks
I'd like to order a dictionary by the value. It seems the way it would usually be done in .Net doesn't exist in the Mono API.
Is there any particular API call or should I do this myself?
Thanks
As commented, the Dictionary class has no ordering. If you want a Dictionary that is ordered, there is the SortedDictionary class.
But the SortedDictionary is ordered by it's keys. Keeping an ordering dictionary by it's values doesn't seem an usual task.
Anyway, if you want to access the values sorted you could do:
dict.Values.OrderBy(v => v).ToList(); //Sorted list of the values of the dictionary dict
or
dict.OrderBy(kvp => kvp.Value).ToList(); //Sorted list (by value) of the key value pairs of the dictionary dict
you can use Linq:
var dict = new Dictionary<string, int>();
var sorted = dict.OrderBy(kvp => kvp.Value).ToList();