我经常需要根据值对字典(由键和值组成)进行排序。例如,我有一个单词和相应频率的散列,我想按频率排序。
有一个SortedList,它适用于单个值(比如频率),我想把它映射回单词。
SortedDictionary按键排序,而不是按值。有些人求助于自定义课程,但有没有更干净的方法呢?
我经常需要根据值对字典(由键和值组成)进行排序。例如,我有一个单词和相应频率的散列,我想按频率排序。
有一个SortedList,它适用于单个值(比如频率),我想把它映射回单词。
SortedDictionary按键排序,而不是按值。有些人求助于自定义课程,但有没有更干净的方法呢?
当前回答
你可以用:
Dictionary<string, string> dic= new Dictionary<string, string>();
var ordered = dic.OrderBy(x => x.Value);
return ordered.ToDictionary(t => t.Key, t => t.Value);
其他回答
在较高的层次上,您别无选择,只能遍历整个Dictionary并查看每个值。
也许这有帮助: http://bytes.com/forum/thread563638.html 复制/粘贴自John Timney:
Dictionary<string, string> s = new Dictionary<string, string>();
s.Add("1", "a Item");
s.Add("2", "c Item");
s.Add("3", "b Item");
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(s);
myList.Sort(
delegate(KeyValuePair<string, string> firstPair,
KeyValuePair<string, string> nextPair)
{
return firstPair.Value.CompareTo(nextPair.Value);
}
);
使用LINQ:
Dictionary<string, int> myDict = new Dictionary<string, int>();
myDict.Add("one", 1);
myDict.Add("four", 4);
myDict.Add("two", 2);
myDict.Add("three", 3);
var sortedDict = from entry in myDict orderby entry.Value ascending select entry;
这也为你提供了很大的灵活性,你可以选择前10%,20 - 10%等等。或者,如果您正在使用词频索引进行提前输入,您还可以包括StartsWith子句。
如果您想要的只是一个按值排序的“临时”列表,那么其他答案都很好。然而,如果你想让一个按Key排序的字典自动与另一个按Value排序的字典同步,你可以使用Bijection<K1, K2>类。
双射<K1, K2>允许您使用两个现有字典初始化集合,因此如果您希望其中一个字典是无序的,而另一个字典是有序的,您可以使用如下代码创建双射
var dict = new Bijection<Key, Value>(new Dictionary<Key,Value>(),
new SortedDictionary<Value,Key>());
您可以像使用任何普通字典一样使用dict(它实现了dicictionary <K, V>),然后调用dict。以获得“逆”字典,它是由值排序。
Bijection<K1, K2>是Loyc.Collections.dll的一部分,但如果您愿意,您可以简单地将源代码复制到您自己的项目中。
注意:如果有多个键具有相同的值,您不能使用双射,但您可以手动同步普通Dictionary<Key, value >和BMultiMap< value, Key>。
你可以按值对Dictionary进行排序,并使用下面的代码在Dictionary中获取结果:
Dictionary <<string, string>> ShareUserNewCopy =
ShareUserCopy.OrderBy(x => x.Value).ToDictionary(pair => pair.Key,
pair => pair.Value);
获得一个排序字典最简单的方法是使用内置的SortedDictionary类:
//Sorts sections according to the key value stored on "sections" unsorted dictionary, which is passed as a constructor argument
System.Collections.Generic.SortedDictionary<int, string> sortedSections = null;
if (sections != null)
{
sortedSections = new SortedDictionary<int, string>(sections);
}
sortedSections将包含section的排序版本