请问SortedDictionary如何按值排序输出?
由大到小排序输出.最好来了简单明了的例子,谢谢.
[解决办法]
根据 Count 反过来 for 就可以了,还需要利用 Linq 的 ElementAt, 话说怎么不用 SortedList
for (var i = dict.Keys.Count-1; i>=0; i--)
{
...
}
- C# code
var dict = new SortedDictionary<int, string>();dict.Add(1, "1");dict.Add(3, "3");dict.Add(2, "2");dict.Add(0, "0");for (int i = dict.Keys.Count-1; i >= 0; i--) Console.WriteLine(dict.ElementAt(i));Console.Read();
[解决办法]
- C# code
SortedDictionary<double, int> results = new SortedDictionary<double, int>(); results.Add(1.5,10); results.Add(0.89,8); results.Add(1.23,20); results.Add(9.55,7); results.Add(4.5,13); var query=results.OrderByDescending(t=>t.Value).ToDictionary(t=>t.Key,t=>t.Value);
[解决办法]
传一个自定义的 IComparer 接口。
http://topic.csdn.net/u/20120111/22/96e3528a-a1c0-4a33-82af-0ea21fb6b621.html