Kal*_*lid 770 .net c# sorting dictionary
我经常需要按值排序字典,包括键和值.例如,我有一个单词的散列和各自的频率,我想按频率排序.
有一个SortedList对单个值(比如频率)有好处,我想将它映射回单词.
SortedDictionary按键排序,而不是值.有些人诉诸于自定义课程,但是有更清洁的方法吗?
car*_*den 509
使用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;
Run Code Online (Sandbox Code Playgroud)
这也可以提供很大的灵活性,你可以选择前10个,20个10%等.或者如果你使用你的单词频率索引type-ahead,你也可以包括StartsWith条款.
Leo*_*ick 507
使用:
using System.Linq.Enumerable;
...
List<KeyValuePair<string, string>> myList = aDictionary.ToList();
myList.Sort(
delegate(KeyValuePair<string, string> pair1,
KeyValuePair<string, string> pair2)
{
return pair1.Value.CompareTo(pair2.Value);
}
);
Run Code Online (Sandbox Code Playgroud)
由于您的目标是.NET 2.0或更高版本,因此您可以将其简化为lambda语法 - 它等效,但更短.如果您的目标是.NET 2.0,那么只有在使用Visual Studio 2008(或更高版本)的编译器时才能使用此语法.
var myList = aDictionary.ToList();
myList.Sort((pair1,pair2) => pair1.Value.CompareTo(pair2.Value));
Run Code Online (Sandbox Code Playgroud)
sea*_*ean 237
var ordered = dict.OrderBy(x => x.Value);
Run Code Online (Sandbox Code Playgroud)
Kal*_*lid 160
环顾四周,并使用一些C#3.0功能,我们可以这样做:
foreach (KeyValuePair<string,int> item in keywordCounts.OrderBy(key=> key.Value))
{
// do something with item.Key and item.Value
}
Run Code Online (Sandbox Code Playgroud)
这是我见过的最干净的方式,类似于Ruby处理哈希的方式.
Mat*_*ear 153
您可以按值对字典进行排序并将其保存回自身(这样当您对其进行操作时,值将按顺序排出):
dict = dict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);
Run Code Online (Sandbox Code Playgroud)
当然,它可能不正确,但它的工作原理.
Mic*_*tum 60
在较高的层次上,您没有其他选择可以遍历整个词典并查看每个值.
也许这会 有所帮助: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);
}
);
Run Code Online (Sandbox Code Playgroud)
Rog*_*cks 24
无论如何,你永远无法对字典进行排序.他们实际上没有订购.字典的保证是键和值集合是可迭代的,并且可以通过索引或键检索值,但这里不保证任何特定的顺序.因此,您需要将名称值对添加到列表中.
Zar*_*dan 18
您不对字典中的条目进行排序..NET中的字典类是作为哈希表实现的 - 根据定义,此数据结构不可排序.
如果您需要能够迭代您的集合(按键) - 您需要使用SortedDictionary,它实现为二进制搜索树.
在您的情况下,源结构是无关紧要的,因为它按不同的字段排序.您仍然需要按频率对其进行排序,并将其放入按相关字段(频率)排序的新集合中.所以在这个集合中,频率是键,单词是值.由于许多单词可以具有相同的频率(并且您将其用作键),因此您既不能使用Dictionary也不能使用SortedDictionary(它们需要唯一键).这将为您提供SortedList.
我不明白为什么你坚持维护主/第一本词典中原始项目的链接.
如果集合中的对象具有更复杂的结构(更多字段),并且您需要能够使用几个不同的字段作为键来有效地访问/排序它们 - 您可能需要一个自定义数据结构,该结构将由主存储组成支持O(1)插入和删除(LinkedList)和几个索引结构--Dictionaries/SortedDictionaries/SortedLists.这些索引将使用复杂类中的一个字段作为键,并将LinkedList中LinkedListNode的指针/引用用作值.
您需要协调插入和删除以使索引与主集合(LinkedList)保持同步,并且删除将是相当昂贵的我认为.这与数据库索引的工作方式类似 - 它们非常适合查找,但当您需要执行许多限制和删除时,它们会成为负担.
如果您要进行一些查找重处理,上述所有内容都是合理的.如果您只需要按频率排序就输出它们,那么您只需生成一个(匿名)元组列表:
var dict = new SortedDictionary<string, int>();
// ToDo: populate dict
var output = dict.OrderBy(e => e.Value).Select(e => new {frequency = e.Value, word = e.Key}).ToList();
foreach (var entry in output)
{
Console.WriteLine("frequency:{0}, word: {1}",entry.frequency,entry.word);
}
Run Code Online (Sandbox Code Playgroud)
mrf*_*lka 14
Dictionary<string, string> dic= new Dictionary<string, string>();
var ordered = dic.OrderBy(x => x.Value);
return ordered.ToDictionary(t => t.Key, t => t.Value);
Run Code Online (Sandbox Code Playgroud)
myt*_*thz 11
或者为了好玩,你可以使用一些LINQ扩展优点:
var dictionary = new Dictionary<string, int> { { "c", 3 }, { "a", 1 }, { "b", 2 } };
dictionary.OrderBy(x => x.Value)
.ForEach(x => Console.WriteLine("{0}={1}", x.Key,x.Value));
Run Code Online (Sandbox Code Playgroud)
las*_*iya 11
排序值
这显示了如何对Dictionary中的值进行排序.我们看到一个可以在Visual Studio中编译并运行的控制台程序.它为Dictionary添加了键,然后按其值对它们进行排序.请记住,Dictionary实例最初不以任何方式排序.我们在查询语句中使用LINQ orderby关键字.
对字典[C#]进行排序的OrderBy子句程序
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Example dictionary.
var dictionary = new Dictionary<string, int>(5);
dictionary.Add("cat", 1);
dictionary.Add("dog", 0);
dictionary.Add("mouse", 5);
dictionary.Add("eel", 3);
dictionary.Add("programmer", 2);
// Order by values.
// ... Use LINQ to specify sorting by value.
var items = from pair in dictionary
orderby pair.Value ascending
select pair;
// Display results.
foreach (KeyValuePair<string, int> pair in items)
{
Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
}
// Reverse sort.
// ... Can be looped over in the same way as above.
items = from pair in dictionary
orderby pair.Value descending
select pair;
}
}
Run Code Online (Sandbox Code Playgroud)
产量
dog: 0
cat: 1
programmer: 2
eel: 3
mouse: 5
Run Code Online (Sandbox Code Playgroud)
使用VB.NET 对SortedDictionary列表进行排序以绑定到ListView控件中:
Dim MyDictionary As SortedDictionary(Of String, MyDictionaryEntry)
MyDictionaryListView.ItemsSource = MyDictionary.Values.OrderByDescending(Function(entry) entry.MyValue)
Public Class MyDictionaryEntry ' Need Property for GridViewColumn DisplayMemberBinding
Public Property MyString As String
Public Property MyValue As Integer
End Class
Run Code Online (Sandbox Code Playgroud)
XAML:
<ListView Name="MyDictionaryListView">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=MyString}" Header="MyStringColumnName"></GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding Path=MyValue}" Header="MyValueColumnName"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Run Code Online (Sandbox Code Playgroud)
小智 5
获取已排序字典的最简单方法是使用内置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);
}
Run Code Online (Sandbox Code Playgroud)
sortedSections 将包含排序版本 sections
如果您想要的是具有按值排序的"临时"列表,则其他答案都很好.但是,如果你想拥有排序字典来Key是自动同步与被排序另一个字典Value,你可以使用Bijection<K1, K2>类.
Bijection<K1, K2> 允许您使用两个现有字典初始化集合,因此如果您希望其中一个字典未排序,并且您希望对另一个字典进行排序,则可以使用以下代码创建您的双射
var dict = new Bijection<Key, Value>(new Dictionary<Key,Value>(),
new SortedDictionary<Value,Key>());
Run Code Online (Sandbox Code Playgroud)
您可以使用dict任何普通字典(它实现IDictionary<K, V>),然后调用dict.Inverse以获取排序依据的"逆"字典Value.
Bijection<K1, K2>是Loyc.Collections.dll的一部分,但如果你愿意,你可以简单地将源代码复制到你自己的项目中.
注意:如果有多个键具有相同的值,则无法使用Bijection,但您可以手动在普通Dictionary<Key,Value>和a 之间进行同步BMultiMap<Value,Key>.
实际上在 C# 中,字典没有 sort() 方法。由于您对按值排序更感兴趣,因此在提供键之前您无法获取值。简而言之,您需要使用 LINQ 遍历它们OrderBy(),
var items = new Dictionary<string, int>();
items.Add("cat", 0);
items.Add("dog", 20);
items.Add("bear", 100);
items.Add("lion", 50);
// Call OrderBy() method here on each item and provide them the IDs.
foreach (var item in items.OrderBy(k => k.Key))
{
Console.WriteLine(item);// items are in sorted order
}
Run Code Online (Sandbox Code Playgroud)
你可以做一招:
var sortedDictByOrder = items.OrderBy(v => v.Value);
Run Code Online (Sandbox Code Playgroud)
或者:
var sortedKeys = from pair in dictName
orderby pair.Value ascending
select pair;
Run Code Online (Sandbox Code Playgroud)
它还取决于您存储的值类型:单个(如字符串、整数)或多个(如列表、数组、用户定义的类)。如果它是单身,你可以列出它,然后应用排序。
如果它是用户定义的类,那么该类必须实现 IComparable,ClassName: IComparable<ClassName>并重写,compareTo(ClassName c)因为它们比 LINQ 更快、更面向对象。
| 归档时间: |
|
| 查看次数: |
567396 次 |
| 最近记录: |