使用过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. C#创建windows服务搭配定时器Timer使用实例(用代码做,截图版)

       功能说明:C#创建一个windows服务,服务启动时D:\mcWindowsService.txt写入数据,服务运行期间每隔两秒写入当前时间. 原理这些就不说了,三语两语说不清楚,直接贴一个实例 ...

  2. 都是iconv惹的祸

    今天在做采集的时候发现只取到了网页的部分内容,当时我就郁闷了,之前都用的采集都可以采集到网页的所有内容,但这次死活就取到部分内容.寻找原因才知道原来是iconv惹的祸. 发现问题时,网上搜了搜,才发现 ...

  3. iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能

    UItableView分组功能 class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableVi ...

  4. 你真的会使用SQL Server的备份还原功能吗?之二:主要备份类型

    假设在下面几个时间段中,一个数据库积累插入了如下数据: 1.完整数据库备份 故名思意,完整数据库备份包括完整的数据库信息.它包括数据库的数据文件和备份结尾的部份活动事务日志. 完整备份基本语法如下: ...

  5. WCF和WPF读取xml的路径问题

    使用WCF编写服务,涉及到xml的读取,使用了System.AppDomain.CurrentDomain.BaseDirectory来获取路径,获得的是项目的基目录 例如: string path ...

  6. debian7 编译qtopia错误解决案例

    问题: kernel/qjpegio.cpp:60:21: error: jpeglib.h: No such file or directory 解决 sudo apt-get install li ...

  7. LeetCode5 Longest Palindromic Substring

    题意: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...

  8. c#基础--常量(const),只读字段(readonly)

    1.0:常量 常量被关键字const 所修饰 我们来看看常量的demo class Program { static void Main(string[] args) { const string n ...

  9. 快递鸟物流单号自动识别接口JAVA对接demo

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  10. WPF/Silverlight Layout 系统概述——Arrange(转)

    Arrange过程概述 普通基类属性对Arrange过程的影响 我们知道Measure过程是在确定DesiredSize的大小,以便Arrange过程参考这个DesiredSize,确定给MyPane ...