项目中可能需要用到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(字典)的键、值排序的更多相关文章

  1. Python 关于列表字典的键值修改

    list (修改列表的索引值) 循环一个列表时,最好不要对原列表有改变大小的操作,这样会影响你的最终结果. #使用负索引进行修改列表 print('First') lis = [11, 22, 33, ...

  2. C#基础精华03(常用类库StringBuilder,List<T>泛型集合,Dictionary<K , V> 键值对集合,装箱拆箱)

    常用类库StringBuilder StringBuilder高效的字符串操作 当大量进行字符串操作的时候,比如,很多次的字符串的拼接操作. String 对象是不可变的. 每次使用 System. ...

  3. python 取出字典的键或者值/如何删除一个字典的键值对/如何遍历字典

    先定义一个字典并直接进行初始化赋值 my_dict = dict(name="lowman", age=45, money=998, hourse=None) 1.取出该字典所有的 ...

  4. Python 字典(键值对)

    Python 字典(键值对) 创建字典 特性:字典中的键不能变,而且唯一 格式:变量名={"键1":值1,"键2":值2} 函数 作用 dict() 强制转换为 ...

  5. Dictionary<k,v>键值对的使用

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Dict ...

  6. Java Dictionary 类存储键值

    字典(Dictionary) 字典(Dictionary) 类是一个抽象类,它定义了键映射到值的数据结构. 当你想要通过特定的键而不是整数索引来访问数据的时候,这时候应该使用Dictionary. 当 ...

  7. python(46):对字典进行排序,多键值排序

    注:改于2017-12-07,python3 下 Removed dict.iteritems(), dict.iterkeys(), and dict.itervalues(). Instead: ...

  8. python3排序 sorted(key=lambda)--实现对字典按value值排序

    使用python对列表(list)进行排序,说简单也简单,说复杂也复杂,我一开始学的时候也搞不懂在说什么,只能搜索一些英文文章看看讲解,现在积累了一些经验,写在这里跟大家分享, 1.sorted函数首 ...

  9. html中用变量作为django字典的键值

    若字典为dic={'name': Barbie, 'age': 20},则在html中dic.name为Barbie,dic.age为20. 但若字典为dic={'Barbie': 1, 'Roger ...

  10. vue中如果在页面中v-model的是字典,那么在定义字典的时候,需要明确定义键值为''或者[],否则给字典的键值赋值后页面不显示

    如题 在template模板中 {{}} {{form_temp.blOwnerMemberList}} #是字典的形式哦 {{}} 在return的属性中 form_temp: { blOwnerM ...

随机推荐

  1. iOS 关于BTC 一些知识点

    1.BTC 用这个网 可以校验 自己的库生成的助记词 地址 是否是合法正常的 https://iancoleman.io/bip39/ 2.知晓 BTC 钱包是否有钱 和交易记录 https://te ...

  2. spark[源码]-SparkEnv执行环境创建

    sparkEnv概述 sparkEnv是spark的执行环境,其中包括众多与Executor执行相关的对象.在local模式下Driver会创建Executor,local-cluster部署模式或者 ...

  3. linux下不同服务器间数据传输(wget,scp)

    一.wget是Linux下最常用的http/ftp文件下载工具1.wget断点续传,只需要加上-c参数即可,例如:代码:wget-chttp://www.abc.com/abc.zip-Oabc.zi ...

  4. ultraedit 查看文件

    转自:https://wenda.so.com/q/1481655902726192 1 UltraEdit在打开文件的时候,会对文件类型进行检查.如果是二进制文件,会自动转为16进制显示模式.如下图 ...

  5. [转]MySQL查看数据库相关信息

    原文链接:MySQL查看数据库相关信息 使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql& ...

  6. win7_32下编译FFmpeg

    运行环境:  VC2010软件:                 [附:本文所用软件安装包:http://download.csdn.NET/detail/sinat_36666600/9705438 ...

  7. Python面试题之Python迭代器

    要理解迭代器,首先要从字面意思来说. 迭代 重复 下一次重复基于上一次的结果 软件开发就是典型的迭代更新. 讲迭代,我们就先来模拟一下迭代: 现在让我们使用while循环来遍历出一个列表list1 = ...

  8. webservice、WSDL简介

    Webservice是跨平台.跨语言的远程调用技术 通信机制的本质是xml数据交换 采用soap协议进行通信 而WSDL 指网络服务描述语言 (Web Services Description Lan ...

  9. keepalived结合lvs

    ip划分: RS1:192.168.223.135 RS2:192.168.223.137 节点node1和node2的keepalived(node1:192.168.223.136,node2:1 ...

  10. LeetCode——largest-rectangle-in-histogram1

    Question Given n non-negative integers representing the histogram's bar height where the width of ea ...