C#分别在集合库和泛型库中有共2套SortedList
以较新的泛型库为例,
SortedList<int, string> l = new SortedList<int, string>();
l.Add(5,"fifth");
l.Add(2, "second");
l.Add(3, "third");
l.Add(1, "first");
l.Add(4, "fourth");
//方法1,加载Linq后,使用扩展Reverse()方法
foreach (KeyValuePair<int, string> kvp in l.Reverse())
Console.WriteLine("K:{0}, V:{1}", kvp.Key, kvp.Value);
//方法2,不用linq,使用枚举器的倒序
foreach (KeyValuePair<int, string> kvp in Enumerable.Reverse(l))
Console.WriteLine("K:{0}, V:{1}", kvp.Key, kvp.Value);
K:5, V:fifth
K:4, V:fourth
K:3, V:third
K:2, V:second
K:1, V:first