I have a dictionary in the form of: { "honda" : 4, "toyota": 7, "ford" : 3, "chevy": 10 }
I want to sort it by the second column aka (the value) descending.
Desired output:
"chevy", 10
"toyota", 7
"honda", 4
"ford", 3
I have a dictionary in the form of: { "honda" : 4, "toyota": 7, "ford" : 3, "chevy": 10 }
I want to sort it by the second column aka (the value) descending.
Desired output:
"chevy", 10
"toyota", 7
"honda", 4
"ford", 3
Actually, if it is HashTable, it can not be sorted. On the other hand, if you have an ArrayList or any other collection that can be sorted, you can implement your own IComparer.
public class MyDicComparer : IComparer
{
public int Compare(Object x, Object y)
{
int Num1= ((Dictionary)x).Value; // or whatever
int Num2= ((Dictionary)y).Value;
if (Num1 < Num2) return 1;
if (Nun1 > Num2) return -1;
return 0; // Equals, must be consideres
}
ArrayList AL;
...
AL.Sort(MyDicComparer);
HTH
Thanks to caryden from: http://stackoverflow.com/questions/289/how-do-you-sort-a-c-dictionary-by-value/1332#1332
Dim sortedDict = (From entry In dict Order By entry.Value Descending Select entry)
The issues reported above were due to improper looping.