使用过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>排序的更多相关文章

  1. SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关

    SortedDictionary<TKey,TValue>能对字典排序 using System; using System.Collections.Generic; using Syst ...

  2. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

  3. SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。

    SortedDictionary<TKey, TValue> 类   表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...

  4. 浅谈Dictionary用法

    一.基础篇 1.Dictionary泛型类提供了从一组键到一组值的映射,即键和值的集合类. 2.Dictionary通过键来检索值的速度是非常快的,这是因为 Dictionary 类是作为一个哈希表来 ...

  5. 269. Alien Dictionary火星语字典(拓扑排序)

    [抄题]: There is a new alien language which uses the latin alphabet. However, the order among letters ...

  6. C# Dictionary 的几种遍历方法,排序

    Dictionary<string, int> list = new Dictionary<string, int>(); list.Add(); //3.0以上版本 fore ...

  7. C# SortedDictionary以及SortedList的浅谈

    msdn叙述:The SortedDictionary<TKey, TValue> generic class is a binary search tree with O(log n) ...

  8. ArrayList、HashTable、List、Dictionary的演化及如何选择使用

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

  9. ArrayList、HashSet、HashTable、List、Dictionary的区别

    在C#中,数组由于是固定长度的,所以常常不能满足我们开发的需求. 由于这种限制不方便,所以出现了ArrayList. ArrayList.List<T> ArrayList是可变长数组,你 ...

随机推荐

  1. java 计算一个方法的返回执行时间

    开始时间   long startTime = System.currentTimeMillis(); 程序业务逻辑代码() 结束时间  long endTime = System.currentTi ...

  2. # 36氪开放日 • 杭州 • 11月10日 # 谈谈参会感受

           今天下午,第一次去参加了36氪的开放日,虽然站着听有点累,但是也很值得.会上很多创业者都分享和展示了他们的产品,一方面自己了解了一些产品人的故事,另一方面也对如何做产品有了新的认识.参会 ...

  3. C和C++函数互相调用

    Call C++ function from C & Call C function from C++ (C和C++函数互相调用) By williamxue on Jun 12, 2007 ...

  4. NDK debug模式

    NDK默认是使用NDEBUG宏的,assert也默认不生效,若要开启assert,按以下步骤: 1.編譯NDK代碼時,後面加上NDK_DEBUG=1 ,如: ndk-build NDK_BUILD=1 ...

  5. 计划任务命令crontab、at

    一.为计划任务提供支持 开始为系统建立计划任务之前,需要为系统添加相关设置,以确保计划任务能够正确运行.计划任务需要的支持主要包括两个方面:正确运行系统服务.准确的系统时间. 1.正确运行系统服务 为 ...

  6. nie题目-游戏排行榜设计

    一个mmorpg游戏,玩家众多,需要对玩家战斗力进行排行,并且战斗力变化时需要及时刷新.需要设计一个这样的排行榜. 关于海量数据排行榜的做法,云风在他的博客里给过思路,谈谈陌陌争霸在数据库方面踩过的坑 ...

  7. LeetCode31 Next Permutation

    题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...

  8. UIView的生命周期

    一. 大体流程: (loadView/nib)文件来加载view到内存-->viewDidLoad函数进一步初始化这些view-->内存不足时, 调用viewDidUnload函数释放vi ...

  9. Web Service 之 开发、部署

    一.C#开发WebService 在 VS2010 中新建 ASP.NET Web 应用程序,取名 WebTest. 应用程序下新建项其实最简单的就是建一个网站项目,直接" 添加新项→Web ...

  10. 1.5.6 Filters

    Filters 过滤器filter应该跟在tokenizer或者另一个filter之后.因为它们将TokenStream作为输入源. <fieldType name="text&quo ...