a: System.Collections.Generic的SortedList
b.: System.Collections.Generic的SortedDictionary
1.a与b的作用:
能够存储数据并自动按照key进行排序。
2.定义与使用:
SortedDictionary<key,value>
SortedList<key,value>
其中key与排序有关,value为值且可以为值或引用类型。
// SortedDictionary
            SortedDictionary<int, string> sDictionary = new SortedDictionary<int, string>();
            sDictionary.Add(3, "cc");
            sDictionary.Add(4, "dd");
            sDictionary.Add(1, "aa");
            sDictionary.Add(2, "bb");            // 此处可以发现sDictionary已经自动按key进行了排序
// 得到SortedDictionary的所有key的集合
SortedDictionary<int, string>.KeyCollection keys = sDictionary.Keys;
            //  得到SortedDictionary的所有value的集合
            SortedDictionary<int, string>.ValueCollection values = sDictionary.Values; 
//SortedList
            SortedList<int, string> sList = new SortedList<int, string>();
            sList.Add(3, "cc");
            sList.Add(4, "dd");
            sList.Add(1, "aa");
            sList.Add(2, "bb");                       // 此处可以发现sList已经自动按key进行了排序
// 得到SortedList的所有key的集合
IList<int> keysList = sList.Keys;
           //  得到SortedList的所有value的集合
            IList<string> valuesList = sList.Values;
3.二者区别
SortedList 泛型类是具有 O(log n) 检索的二进制搜索树,其中 n 是字典中元素的数目。就这一点而言,它与 SortedDictionary 泛型类相似。这两个类具有相似的对象模型,并且都具有 O(log n) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:
- 
    
SortedList 使用的内存比 SortedDictionary 少。
 - 
    
SortedDictionary 可对未排序的数据执行更快的插入和移除操作,它的运算复杂度为 O(log n),而 SortedList 的运算复杂度为 O(n)。
 - 
    
如果使用排序数据一次性填充列表,则 SortedList 比 SortedDictionary 快。
 
4.与List相比的优势是能够自动排序
List的排序请看另一篇文章。
相关新闻
- 小程序登录流程图理解 2020-08-18
 - 在C#中获取web.config中的配置信息 2021-08-23
 - 小程序open-data头像样式 2021-04-10
 - 小程序rich-text 富文本解析图片过大和图片路径的问题 2020-11-25
 - C#中去掉字符串的最后一个字符 2020-11-23
 
                
                        
                    
