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);

时间: 2024-08-28 23:49:51

SortedDictionary的相关文章

SortedDictionary&lt;TKey,TValue&gt;正序与反序排序

SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SortDictionary { class Program { static void Main(string[] args) { TestDictionarySort()

字典---有序字典(SortedDictionary&lt;TKey,TValue&gt;)

--------------------------------------------------------------EmployeeID.cs(键) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; public class EmployeeID : ICo

SortedDictionary&lt;TKey, TValue&gt; 类 表示根据键进行排序的键/值对的集合。

SortedDictionary<TKey, TValue> 类   表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每个键必须是唯一的. 键不能为 null,但是如果值类型 TValue 为引用类型,该值则可以为空.SortedDictionary 可对未排序的数据执行更快的插入和移除操作.

记一次SortedDictionary的不当使用

起初想用SortedDictionary做游戏中的排行榜,代码如下: using UnityEngine; using System; using System.Collections; using System.Collections.Generic; public class CustomComparer<T> : IComparer<T> { Func<T, T, int> mComparerFunc; public CustomComparer(Func<

C# 谈Dictionary&lt;TKey,TValue&gt;,SortedDictionary&lt;TKey,TValue&gt;排序

使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary, 但SortedDictionary并不是那么理想,其默认的方式只支持正序排序,想要反序排序时必须得靠自己重新编写代码,下面来看一个简单的例子: private void TestDictionarySort() { SortedDictionary<string, string> sd = new SortedDictionary<string

Net集合类的研究-有序集合(二)-SortedDictionary&lt;TKey,TValue&gt;(转)

Net集合类的研究-有序集合(二)-SortedDictionary<TKey,TValue> 从类名就可以看出SortedDictionary<TKey,TValue>和上篇介绍的SortedList一样,都是有序集合,但从类内部的存储结构上看,两者有很大区别,SortedList内部用数组保存,只能算是有序线性表,而SortedDictionary<TKey,TValue>的内部结构是红黑树. 园子里有不少关于红黑树的好文章,已将红黑树分析的很透彻.所以这里不讨论红

深入解析Hashtable、Dictionary、SortedDictionary、SortedList

我们先看Hashtable. MSDN的解释:表示键/值对的集合,这些键/值对根据键的哈希代码进行组织. Hash算法是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩映射,也就是,散列值的空间通常远小于输入的空间,不 同的输入可能会散列成相同的输出,而不可能从散列值来唯一的确定输入值. Hashtable 对象由包含集合元素的存储桶组成.存储桶是 Hashtable 中各元素的虚拟子组,与大多数集合中进行的搜索和检

C# SortedDictionary以及SortedList的浅谈

msdn叙述:The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedList<TKey, TValue> generic class. The two classes

C# SortedDictionary 排序区分大小写

C# SortedDictionary SortedDictionary<string, string> dic = new SortedDictionary<string, string>(StringComparer.Ordinal); 定义的时候加上 StringComparer.Ordinal  表示区分大小写排序 不加上默认不区分!!!!! 原文地址:https://www.cnblogs.com/MYDoraemon/p/12055577.html