sorting a dictionary by key?
我需要根据它的键对字典进行排序,因为wp7不支持
这个堆栈溢出问题包括使用LINQ的解决方案。
如何按值对词典进行排序?
我试过这个代码:
1 2 3 4 5 6 7 8 9 10 11 | Dictionary<string, string> dict = new Dictionary<string,string>(); dict.Add("3","three"); dict.Add("1","one"); dict.Add("2","two"); var sortedDict = (from entry in dict orderby entry.Key ascending select entry); foreach (var k in sortedDict) { Console.WriteLine("key:{0}, val={1}", k.Key, k.Value); } |
这是可行的,但sorteddict不是字典。我解决了:
1 2 | sortedDict = (from entry in dict orderby entry.Key ascending select entry) .ToDictionary(x => x.Key, x => x.Value); |
号