排序代替字典对于钥匙 [英] Sorting a Dictionary in place with respect to keys

查看:100
本文介绍了排序代替字典对于钥匙的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#字典像

Dictionary<Person, int>

和我想排序那本字典的的相对于键(在Person类的字段)。我该怎么办呢?在互联网上每一个可用的帮助是,与在词典的位置排序没有特别的例子名单。任何帮助将是非常美联社preciated!

and I want to sort that dictionary in place with respect to keys (a field in class Person). How can I do it? Every available help on the internet is that of lists with no particular example of in place sorting of Dictionary. Any help would be highly appreciated!

推荐答案

您不能排序词典&LT; TKEY的,TValue&GT; - 这是本质上无序的。 (或者更确切地说,在哪些条目被检索的顺序是实现特定的,你不应该依赖于它的工作版本之间的相同的方式,因为排序是不是其设计功能的一部分。)

You can't sort a Dictionary<TKey, TValue> - it's inherently unordered. (Or rather, the order in which entries are retrieved is implementation-specific. You shouldn't rely on it working the same way between versions, as ordering isn't part of its designed functionality.)

您的可以的使用 排序列表&LT; TKEY的,TValue&GT ; 或<一个href=\"http://msdn.microsoft.com/en-us/library/f7fta44c%28v=VS.100%29.aspx\"><$c$c>SortedDictionary<TKey, TValue&GT; ,无论是哪种类型的键(一个可配置的方式,如果你传递一个的IEqualityComparer&LT的; T&GT; 进入构造函数) - 也许那些是有用的给你。

You can use SortedList<TKey, TValue> or SortedDictionary<TKey, TValue>, both of which sort by the key (in a configurable way, if you pass an IEqualityComparer<T> into the constructor) - might those be of use to you?

很少注意名称中的单词列表排序列表 - 它仍然是一个字典,它键映射到值。它的实施的使用列表内部,有效地 - 这样,而不是通过哈希code仰视,它确实二进制搜索。 SortedDictionary 同样基于二进制搜索,而是通过一棵树,而不是名单。

Pay little attention to the word "list" in the name SortedList - it's still a dictionary in that it maps keys to values. It's implemented using a list internally, effectively - so instead of looking up by hash code, it does a binary search. SortedDictionary is similarly based on binary searches, but via a tree instead of a list.

这篇关于排序代替字典对于钥匙的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆