`

C#的Dictionary值排序和SortedDictionary键排序

 
阅读更多

对一个Dictionary<TKey, TValue>进行键排序可以直接用SortedDictionary
SortedDictionary<TKey, TValue> 泛型类是检索运算复杂度为 O(log n) 的二叉搜索树,其中 n 是字典中的元素数。 就这一点而言,它与 SortedList<TKey, TValue> 泛型类相似。 这两个类具有相似的对象模型,并且都具有 O(log n) 的检索运算复杂度。
这两个类的区别在于内存的使用以及插入和移除元素的速度:
SortedList<TKey, TValue> 使用的内存比 SortedDictionary<TKey, TValue> 少,SortedDictionary<TKey, TValue> 可对未排序的数据执行更快的插入和移除操作:
它的时间复杂度为 O(log n),而 SortedList<TKey,TValue> 为 O(n),如果使用排序数据一次性填充,SortedList<TKey,TValue>比 SortedDictionary<TKey, TValue> 快。
每个键/值对都可以作为KeyValuePair<TKey, TValue> 结构进行检索,或作为DictionaryEntry通过非泛型IDictionary接口进行检索。只要键用作SortedDictionary<TKey, TValue> 中的键,它们就必须是不可变的。
SortedDictionary<TKey, TValue> 中的每个键必须是唯一的。 键不能为 null,但是如果值类型 TValue 为引用类型,该值则可以为空。
SortedDictionary<TKey, TValue> 需要比较器实现来执行键比较。 可以使用一个接受 comparer 参数的构造函数来指定 IComparer<T> 泛型接口的实现;
如果不指定实现,则使用默认的泛型比较器 Comparer<T>.Default。
如果类型 TKey 实现 System.IComparable<T> 泛型接口,则默认比较器使用该实现。
对一个Dictionary<TKey, TValue>进行值排序可以用LINQ:

Dictionary<string, string> MyDictionary =new Dictionary<string, string>();

MyDictionary
= (from entry in MyDictionary
orderby entry.Value ascending
select entry).ToDictionary(pair
=> pair.Key, pair => pair.Value);

 

分享到:
评论

相关推荐

    C# Dictionary和SortedDictionary的简介

    1、SortedDictionary 泛型类 SortedDictionary 泛型类是检索运算复杂度为 O(log n... SortedDictionary 可对未排序的数据执行更快的插入和移除操作:它的时间复杂度为 O(log n),而SortedList 为 O(n)。 如果使用

    Dictionary, SortedDictionary, SortedList 横向评测

    Dictionary, SortedDictionary, SortedList 横向评测

    C#5.0本质论第四版(因文件较大传的是百度网盘地址)

    16.2.5 已排序集合:SortedDictionary和SortedList 453 16.2.6 栈集合:Stack 454 16.2.7 队列集合:Queue 455 16.2.8 链表:LinkedList 456 16.3 提供索引器 456 16.4 返回null或者空集合 ...

    c#支付宝标准双接口实现

    功能:除去数组中的空值和签名参数并以字母a到z的顺序排序 输入:SortedDictionary, string&gt; dicArrayPre 过滤前的参数组 输出:Dictionary, string&gt; 去掉空值与签名参数后的新签名参数组 public static string ...

    SubHubT:进行中的单线程EventAggregatorEventBroker C#解决方案

    EventBroker C#解决方案去做集中订阅和消息触发期间订阅/取消订阅确定性处理覆盖测试说明自述文件递归触发确定性处理覆盖测试说明自述文件重新排序SortedDictionary时,匹配优先级订阅的测试顺序。 也许引入...

    SubHub:正在进行的单线程EventAggregatorEventBroker C#解决方案

    也许引入Subscrition.creationIndex以获得其他排序值 改善API IoC初始化树 特征 全局和过滤订阅 全局在所有匹配类型的消息上触发 仅在匹配类型和匹配过滤对象的消息上触发过滤 与其他一些解决方案不同,全局和过滤...

    .Net可视化调试工具

    6:泛型系列:Dictionary&lt;,&gt;、LinkedList、List、Queue、SortedDictionary&lt;,&gt;、SortedList&lt;,&gt;、Stack 7:非泛型系列:ArrayList、Hashtable、Queue、SortedList、Stack 8:其它继承自:Enumerable 接口的类型:...

    Bee-tree.zip

    尽管不像.NET的Dictionary ,TValue&gt;类那样使用,但SortedDictionary ,TValue&gt;是BCL集合套件的重要组成部分。 但是,框架类仅根据分类和搜索的二叉树为您提供了一种红黑树算法。 有时,B树,AVL树或八叉树...

    算法是用于创建智能应用程序的工具库。-Swift开发

    功能概率工具期望值可编程概率块数组扩展集扩展数据结构DoublyLinkedList堆栈队列双端队列RedBlackTree SortedSet SortedMultiSet SortedDictionary SortedMultiDictionary要求iOS 8.0+ / Mac OS X 10.9+ Xcode 8.0+...

    BobBuilder_app

    I decided against using SortedDictionary for the pages as it was slower than a normal Dictionary and for the purpose of a key value store the sorted-ness was not need and could be handled in other ...

Global site tag (gtag.js) - Google Analytics