So here is a code sample which can be used to sort a dictionary with respect to its values. I have blended 2 or 3 methods into one so that it takes input as dictionary and gives output as dictionary. Code may be inefficient due to my inabilities in programming but still it works for me. Hopefully for you it would work also. :-)
public static DictionarySort (Dictionary dict)
{
List> list = new List >();
foreach(KeyValuePairkvp in dict)
{
list.Add(kvp);
}
list.Sort(
delegate(KeyValuePairfirstPair,
KeyValuePairnextPair)
{
return nextPair.Value.CompareTo(firstPair.Value);
}
);
Dictionaryd = new Dictionary ();
foreach(KeyValuePairkvp in list)
{
d.Add(kvp.Key, kvp.Value);
}
return d;
}
No comments:
Post a Comment