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是可变长数组,你 ...
随机推荐
- MemCache超详细解读 图
http://www.cnblogs.com/xrq730/p/4948707.html MemCache是什么 MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于 ...
- 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法
编写布局代码时发现xml脚本出现错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”,原来是一个na ...
- qregularexpression和qregexp的区别
QRegularExpression 是Qt 5.0才引进的,相对于QRegExp,QRegularExpression class修复了很多bug,提高了效率,提供了对Perl的RegEx几乎全面兼 ...
- 一款基于jQuery仿淘宝红色分类导航
今天给大家分享一款基于jQuery仿淘宝红色分类导航.这款分类导航适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗.效果图如下: 在线预览 ...
- mysql describe
describe命令一.describe命令用于查看特定表的详细设计信息,例如为了查看guestbook表的设计信息,可用:describe guestbook describe ol_user us ...
- ngrok 2016版
1. 先到http://www.ngrok.cc/下载客户端 2.进管理页面 注册登录 3.绑定域名 新增: 复制客户端id 打开该目录 按住shift 右键->在此处打开命令窗口 输入 sta ...
- LeetCode40 Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...
- 关于MFC库和CRT库冲突的分析
当MFC库和CRT库冲突时,会出现一个LNK2005的错误.具体的错误如下: nafxcwd.lib(dllmodul.obj): error LNK2005: _DllMain@12 already ...
- org.apache.hadoop.conf-Configured
org.apache.hadoop.conf中的最后一个类,也是这个包中以后用的最频繁的一个,Configurable算是肉体,Configuration算是灵魂吧 package org.apach ...
- 下拉选择框加listview删除
package com.downselect; import java.util.ArrayList; import android.R.array; import android.app.Activ ...