对Dictionary按照其Value值做降序排列
使用var dicSort = from d in dic
orderby d.Value descending
select d;
在VS2015不起作用,使用下面方式进行。
Dictionary<string, double> dic = new Dictionary<string, double>();
string t1 = "";
for (int i = 1; i <7; i++)
{
double value = i * 12.5;
dic.Add("key"+i,value);
t1 += value + "\n";
}
Console.WriteLine(t1);
List<KeyValuePair<string, double>> list = new List<KeyValuePair<string, double>>(dic);
list.Sort(delegate (KeyValuePair<string, double> s1, KeyValuePair<string, double> s2)
{
return s2.Value.CompareTo(s1.Value);
});
dic.Clear();
string t2 = "";
foreach (KeyValuePair<string, double> pair in list)
{
dic.Add(pair.Key, pair.Value);
t2 += pair.Value + "\n";
}
Console.WriteLine(t2);
Console.ReadLine();