登 录
Following is an example of using LINQ to sort c# Dictionary by Value:
Dictionary<String, int> itemCount = new Dictionary<string,int>(); itemCount.Add("abc", 2); itemCount.Add("edf", 3); itemCount.Add("gfh", 1); IEnumerable<KeyValuePair<String, int>> sortedDict = from entry in itemCount orderby entry.Value descending select entry; itemCount = sortedDict.ToDictionary(pair => pair.Key, pair => pair.Value); foreach (KeyValuePair<String, int> pair in itemCount) { Console.WriteLine(pair.Key + ":" + pair.Value); }
抱歉!评论已关闭.