C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序
使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary, 但SortedDictionary并不是那么理想,其默认的方式只支持正序排序,想要反序排序时必须得靠自己重新编写代码,下面来看一个简单的例子:
private void TestDictionarySort()
{
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
sd.Add("", "fdsgsags");
sd.Add("acb", "test test");
sd.Add("", "lslgsgl");
sd.Add("2bcd13", "value"); foreach (KeyValuePair<string, string> item in sd)
{
Response.Write("键名:" + item.Key + " 键值:" + item.Value);
} }
上面代码输出效果:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321 键值:fdsgsags
键名:acb 键值:test
test
好了,现在我们来看一下反序排序的效果,请看下面的代码:
private void TestDictionarySort()
{
SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
sd.Add("", "fdsgsags");
sd.Add("acb", "test test");
sd.Add("", "lslgsgl");
sd.Add("2bcd13", "value"); Response.Write("<br />正序排序数据:<br />");
foreach (KeyValuePair<string, string> item in sd)
{
Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />");
} //重新封装到Dictionary里(PS:因为排序后我们将不在使用排序了,所以就使用Dictionary)
Dictionary<string, string> dc = new Dictionary<string, string>();
foreach (KeyValuePair<string, string> item in sd.Reverse())
{
dc.Add(item.Key, item.Value);
}
sd = null;
//再看其输出结果:
Response.Write("<br />反序排序数据:<br />");
foreach (KeyValuePair<string, string> item in dc)
{
Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />");
} }
上面代码输出效果:
正序排序数据:
键名:1123 键值:lslgsgl
键名:2bcd13 键值:value
键名:321
键值:fdsgsags
键名:acb 键值:test test
反序排序数据:
键名:acb 键值:test
test
键名:321 键值:fdsgsags
键名:2bcd13 键值:value
键名:1123 键值:lslgsgl
C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序的更多相关文章
- SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关
SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using Syst ...
- .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...
- SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。
SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...
- 浅谈Dictionary用法
一.基础篇 1.Dictionary泛型类提供了从一组键到一组值的映射,即键和值的集合类. 2.Dictionary通过键来检索值的速度是非常快的,这是因为 Dictionary 类是作为一个哈希表来 ...
- 269. Alien Dictionary火星语字典(拓扑排序)
[抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...
- C# Dictionary 的几种遍历方法,排序
Dictionary<string, int> list = new Dictionary<string, int>(); list.Add(); //3.0以上版本 fore ...
- C# SortedDictionary以及SortedList的浅谈
msdn叙述:The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) ...
- ArrayList、HashTable、List、Dictionary的演化及如何选择使用
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...
- ArrayList、HashSet、HashTable、List、Dictionary的区别
在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...
随机推荐
- SQL 32位还原位64位出现3154错误
1:首先检查新建数据库的路径是否正确. 2:勾选覆盖原有数据库. 3:新建查询,选择master.然后新建查询中进行如下直接对bak文件的操作. RESTORE DATABASE TestFROM D ...
- java学习笔记(3)——面向对象
this关键字 this是当前对象的引用,是运行期间当前对象本身. 可以使用this明确的访问当前对象的属性或者方法,类似于“我” this()可以调用本类的其他构造器,可以使用构造器的重用简化代码的 ...
- 目标检測的图像特征提取之(一)HOG特征
1.HOG特征: 方向梯度直方图(Histogram of Oriented Gradient, HOG)特征是一种在计算机视觉和图像处理中用来进行物体检測的特征描写叙述子.它通过计算和统计图像局部区 ...
- TP复习15
## ThinkPHP 3.1.2 URL#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.URL规则 1.默认是区分大小写的 2.如果我们不想区分大小 ...
- vs2012运行c语言出现:无法查找或打开 PDB 文件
1.点 调试2.然后 选项和设置3.右边勾上 启用源服务器支持4.左边点 符号5.把微软符号服务器勾6.运行的时候等一下 莫慌!!7.问题解决
- 好记心不如烂笔头之jQuery学习,第一章
jQuery对象和DOM对象的转换: 1.jquery对象是对象数组,于是乎: var $cr = $('#cr'); var cr = $cr[0]; 2.使用jquery的自带函数: var $c ...
- QQ群信息统计
接口一: 1:QQ群信息统计 地址:http://localhost:8080/webServices/messageSort 注意:连接地址提交的是一个txt文件,返回是一个list的json字符串 ...
- 关于 Android项目“error: Apostrophe not preceded by \ (”的解决方法
用Eclipse环境开发Android项目,如果编译时控制台报出“error: Apostrophe not preceded by \ (”这种错误,那么多半是因为项目中的一个strings.xml ...
- iso8583报文自学笔记
一.8583报文组成 TPDU 报文头 应用数据 ISO8583 Msg ID 目的 地址 源地址 应用类别定义 软件 总版本号 终端 状态 处理 要求 保留使用(软件分版本号) 交易数据 60H N ...
- 用JDBC查询数据库
JDBC API的核心组件:1.DriverManager类:用语跟踪可用的JDBC驱动程序并产生数据库连接. 2.Connection接口:用于取得数据库信息.生成数据库语句,并管理数据库事务. 3 ...