C# 与vb.net 的Dictionary(字典)的键、值排序
项目中可能需要用到Dictionary 排序,于是先做了一个小demo ,网上搜索真的没有能满足我需要的,都是类似的,于是理解改造,一上午就在查找,实践中过去了。现在把它实现了,把代码贴出来,算是一个笔记吧。希望给需要的人也一个参考。
一、C# 版本
代码
public void gettest()
{
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1.Add("2015-4-01", "2015-4-05");
dic1.Add("2015-4-29", "2015-5-01");
dic1.Add("2015-4-07a", "2015-4-10");
dic1.Add("2015-4-07b", "2015-4-10");
dic1.Add("2015-5-02", "2015-5-08");
dic1.Add("2015-4-11", "2015-4-20");
dic1.Add("2015-4-21", "2015-4-28");
Dictionary<string, string> dic1Asc = dic1.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
Dictionary<string, string> dic1desc = dic1.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
Dictionary<string, string> dic1Asc1 = (from d in dic1 orderby d.Key ascending select d).ToDictionary(k => k.Key, v => v.Value);
Dictionary<string, string> dic1desc2 = (from d in dic1 orderby d.Key descending select d).ToDictionary(k => k.Key, v => v.Value); foreach (KeyValuePair<string, string> kvp in dic1Asc)
{
Response.Write(string.Format("开始日期 = {0}, 结束日期 = {1} <br/>", kvp.Key, kvp.Value));
} } public void gettest2()
{
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1.Add("2015-4-01", "2015-4-05");
dic1.Add("2015-4-29", "2015-5-01");
dic1.Add("2015-4-07a", "2015-4-10");
dic1.Add("2015-4-07b", "2015-4-10");
dic1.Add("2015-5-02", "2015-5-08");
dic1.Add("2015-4-11", "2015-4-20");
dic1.Add("2015-4-21", "2015-4-28"); Response.Write("<br />正序排序数据:<br />");
foreach (KeyValuePair<string, string> item in dic1)
{
Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />");
} Dictionary<string, string> dc = new Dictionary<string, string>(); foreach (KeyValuePair<string, string> kvp in dic1.Reverse())
{
dc.Add(kvp.Key, kvp.Value);
}
dic1 = null;
//再看其输出结果:
Response.Write("<br />反序排序数据:<br />");
foreach (KeyValuePair<string, string> item in dc)
{
Response.Write("键名:" + item.Key + " 键值:" + item.Value + "<br />");
}
} public void gettest3()
{
Dictionary<string, string> dic1 = new Dictionary<string, string>();
dic1.Add("2015-4-01", "2015-4-05");
dic1.Add("2015-4-29", "2015-5-01");
dic1.Add("2015-4-07a", "2015-4-10");
dic1.Add("2015-4-07b", "2015-4-10");
dic1.Add("2015-5-02", "2015-5-08");
dic1.Add("2015-4-11", "2015-4-20");
dic1.Add("2015-4-21", "2015-4-28");
List<KeyValuePair<string, string>> myList = new List<KeyValuePair<string, string>>(dic1);
myList.Sort(delegate(KeyValuePair<string, string> s1, KeyValuePair<string, string> s2)
{
return s1.Value.CompareTo(s2.Value);
});
dic1.Clear();
foreach (KeyValuePair<string, string> pair in myList)
{
dic1.Add(pair.Key, pair.Value);
}
foreach (string key in dic1.Keys)
{
Response.Write(string.Format("开始日期:{0}<br/>",dic1[key]));
} }
效果图:

二、vb.net版本
代码
Public Sub gettest()
Dim dic1 As Dictionary(Of String, String) = New Dictionary(Of String, String)
dic1.Add("2015-4-01", "2015-4-05")
dic1.Add("2015-4-29", "2015-5-01")
dic1.Add("2015-4-07a", "2015-4-10")
dic1.Add("2015-4-07b", "2015-4-10")
dic1.Add("2015-5-02", "2015-5-08")
dic1.Add("2015-4-11", "2015-4-20")
dic1.Add("2015-4-21", "2015-4-28")
Dim myList As List(Of KeyValuePair(Of String, String)) = sortByValue(dic1)
For Each kvp As KeyValuePair(Of String, String) In myList
'Console.WriteLine(kvp.Key & ":" & kvp.Value)
Response.Write(String.Format("开始日期:{0}<br/>", kvp.Key))
Next End Sub Public Sub gettest2()
Dim dic1 As Dictionary(Of String, String) = New Dictionary(Of String, String)
dic1.Add("2015-4-01", "2015-4-05")
dic1.Add("2015-4-29", "2015-5-01")
dic1.Add("2015-4-07a", "2015-4-10")
dic1.Add("2015-4-07b", "2015-4-10")
dic1.Add("2015-5-02", "2015-5-08")
dic1.Add("2015-4-11", "2015-4-20")
dic1.Add("2015-4-21", "2015-4-28")
Dim myList As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))(dic1)
myList.Sort(Function(s1 As KeyValuePair(Of String, String), s2 As KeyValuePair(Of String, String))
Return s1.Value.CompareTo(s2.Value)
End Function)
For Each kvp As KeyValuePair(Of String, String) In myList
'Console.WriteLine(kvp.Key & ":" & kvp.Value)
Response.Write(String.Format("开始日期:{0}<br/>2<br/>", kvp.Key))
Next
End Sub
Shared Function hikaku(ByVal kvp1 As KeyValuePair(Of String, String), ByVal kvp2 As KeyValuePair(Of String, String)) As String
Return kvp1.Value.CompareTo(kvp2.Value)
' Return kvp2.Value - kvp1.Value
End Function
Shared Function sortByValue(ByVal dict As Dictionary(Of String, String)) As List(Of KeyValuePair(Of String, String))
Dim list As New List(Of KeyValuePair(Of String, String))(dict)
list.Sort(AddressOf hikaku)
Return list
End Function
效果图:

C#的理解好一些,vb.net的有点难度,花了不少时间。
参考 :http://www.cnblogs.com/sekihin/archive/2008/08/27/1277605.html
C# 与vb.net 的Dictionary(字典)的键、值排序的更多相关文章
- Python 关于列表字典的键值修改
list (修改列表的索引值) 循环一个列表时,最好不要对原列表有改变大小的操作,这样会影响你的最终结果. #使用负索引进行修改列表 print('First') lis = [11, 22, 33, ...
- C#基础精华03(常用类库StringBuilder,List<T>泛型集合,Dictionary<K , V> 键值对集合,装箱拆箱)
常用类库StringBuilder StringBuilder高效的字符串操作 当大量进行字符串操作的时候,比如,很多次的字符串的拼接操作. String 对象是不可变的. 每次使用 System. ...
- python 取出字典的键或者值/如何删除一个字典的键值对/如何遍历字典
先定义一个字典并直接进行初始化赋值 my_dict = dict(name="lowman", age=45, money=998, hourse=None) 1.取出该字典所有的 ...
- Python 字典(键值对)
Python 字典(键值对) 创建字典 特性:字典中的键不能变,而且唯一 格式:变量名={"键1":值1,"键2":值2} 函数 作用 dict() 强制转换为 ...
- Dictionary<k,v>键值对的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dict ...
- Java Dictionary 类存储键值
字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...
- python(46):对字典进行排序,多键值排序
注:改于2017-12-07,python3 下 Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues(). Instead: ...
- python3排序 sorted(key=lambda)--实现对字典按value值排序
使用python对列表(list)进行排序,说简单也简单,说复杂也复杂,我一开始学的时候也搞不懂在说什么,只能搜索一些英文文章看看讲解,现在积累了一些经验,写在这里跟大家分享, 1.sorted函数首 ...
- html中用变量作为django字典的键值
若字典为dic={'name': Barbie, 'age': 20},则在html中dic.name为Barbie,dic.age为20. 但若字典为dic={'Barbie': 1, 'Roger ...
- vue中如果在页面中v-model的是字典,那么在定义字典的时候,需要明确定义键值为''或者[],否则给字典的键值赋值后页面不显示
如题 在template模板中 {{}} {{form_temp.blOwnerMemberList}} #是字典的形式哦 {{}} 在return的属性中 form_temp: { blOwnerM ...
随机推荐
- 金融 贷款类 App 审核被拒 4.3 1.2 2.1 4.2.2 问题总结
辛辛苦苦搞了一两个月,开发测试修bug,一路艰辛,到了审核这最后一关,各位同仁,咬紧牙关!接下来是鄙人遇到过的被拒问题,望能帮到诸君! ******************************** ...
- javaWeb 使用线程池+队列解决"订单并发"问题
解决方式:使用线程池+队列 项目基于Spring,如果不用spring需要自己把 ThreadPoolManager.java 改成单例模式 1.写一个Controller(Spring mvc) / ...
- jmeter+http接口测试
参考: http://blog.csdn.net/github_27109687/article/details/71968662 Jmeter接口测试+压力测试 http://blog.csdn ...
- 自我管理--拖延 vs 心理
案例1:你在销售公司产品,需要更多客户,这时你收到一条短信,说对你们产品感兴趣,让你马上发送详细资料看看,语气颇为傲慢.你有点恼火,这个人一点都不客气,于是两小时后你才发送过去. 案例2:这个月的 ...
- 20145329 《Java程序设计》实验一总结
实验指导教师:娄嘉鹏老师 实验日期:2016.4.8 实验时间:16:30~18:30 实验序号:实验一 实验名称:Java开发环境的熟悉 实验目的与要求: 使用JDK编译.运行简单的Java程序. ...
- Apache-solr
1.1. 下载 从Solr官方网站(http://lucene.apache.org/solr/ )下载Solr4.10.3,根据Solr的运行环境,Linux下需要下载lucene-4.10.3.t ...
- win10下搭建jz2440v3(arm s3c2440)开发及gdb调试环境【转】
本文转载自:https://blog.csdn.net/newjay03/article/details/72835758 本来打算完全在Ubuntu下开发的,但是水平有限,没有在Ubuntu下找到合 ...
- CentOS 7配置静态IP地址
[root@centos1 ~]# ifconfig -bash: ifconfig: command not found 首先,习惯性的输入echo $PATH(查看当前PATH环境变量,跟DOS的 ...
- PHP 根据IP地址获取所在城市
header('Content-Type:text/html;Charset=utf-8'); function GetIp(){ $realip = ''; $unknown = 'unknown' ...
- C# Programming Guide-->Statements, Expressions, and Operators-->Anonymous Functions
C# Programming Guide Anonymous Functions Lambda Expressions Anonymous Methods In versions of C# befo ...