用LINQ对Dictionary排序-创新互联
代码如下 :
分享文章:用LINQ对Dictionary排序-创新互联
文章起源:http://scpingwu.com/article/doppoc.html

static void Main(string[] args)
{
Dictionary di = new Dictionary();
di.Add(9, 1);
di.Add(0, 6);
di.Add(2, 8);
var sortedDict = (from entry in di orderby entry.Key ascending select entry)
.ToDictionary(pair=> pair.Key, pair => pair.Value);
foreach (var pair in di)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.WriteLine("--------------------");
foreach (var pair in sortedDict)
{
Console.WriteLine("{0},{1}", pair.Key, pair.Value);
}
Console.ReadLine();
}
方法来自stackoverflow
http://stackoverflow.com/questions/289/how-do-you-sort-a-c-sharp-dictionary-by-value
分享文章:用LINQ对Dictionary排序-创新互联
文章起源:http://scpingwu.com/article/doppoc.html