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是可变长数组,你 ...
随机推荐
- Codeforces Beta Round #6 (Div. 2 Only) E. Exposition multiset
E. Exposition Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/ ...
- Android输入输出机制之来龙去脉
openInputChannelPair( 阅读本文的前提条件是知道匿名管道和匿名共享内存是怎么一回事,否则阅读相应的文章. Anonymous pipes 和Anonymous Shared Mem ...
- WCF摘记
//绑定形式 NetTcpBinding bind = new NetTcpBinding(); //地址 EndpointAddress address = new EndpointAddress( ...
- Media Queries详细
@media only screen and (max-device-width: 480px) { //页面最大宽度480px } <link rel="stylesheet&quo ...
- 解决ajax.net 1.0中文乱码问题!
在使用ajax.net的UpdatePanel的时候,当requestEncoding编码为GB2312的时候,出现乱码.如果要解决这个问题最简单的就是改用utf-8了,但是原来使用GB2312, ...
- 一款基于jQuery的支持鼠标拖拽滑动焦点图
记得之前我们分享过一款jQuery全屏广告图片焦点图,图片切换效果还不错.今天我们要分享另外一款jQuery焦点图插件,它的特点是支持鼠标拖拽滑动,所以在移动设备上使用更加方便,你只要用手指滑动屏幕即 ...
- Win7下安装Apache+PHP+phpMyAdmin+wordpress+drupal+discuz
[Apache] Listen 90 DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs&q ...
- iOS开发中添加PrefixHeader.pch要注意的问题
在Xcode6.0已经不默认生成PrefixHeader.pch文件了,而PrefixHeader.pch文件对我们开发带来的便利性是不言而喻的,所以我们怎么在工程中添加PrefixHeader.pc ...
- ImageSource使用心得(转)
很多时候,我们会使用图片来装饰UI,比如作为控件背景等. 而这些图片可以分为两种形式,即存在于本地文件系统中的图片和存在于内存中的图片 对于这两种形式的图片,在WPF中,使用方法不同,下面主要说明针对 ...
- IOS NS 字符串 数组 字典 文件 动态 静态 操作
ios 常用字符串的操作 //将NSData转化为NSString NSString* str = [[NSString alloc] initWithData:response e ...