项目中可能需要用到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. 无线路由和无线AP的区别

    一.答疑解惑 1.什么是无线AP? AP:Access Point 接入点 无线AP:无线接入点是一个无线网络的接入点,俗称“热点”.主要有路由交换接入一体设备和纯接入点设备,一体设备执行接入和路由工 ...

  2. EXTJS 下载

    Extjs各版本的下载链接 Extjs的版本繁多,本文收集了Extjs各个版本的下载链接,包括官网和非官网的,以及各种汉化版api,欢迎大家下载分享. Extjs最新版下载链接:http://www. ...

  3. $python日期和时间的处理

    总结一下python中对日期和时间的常用处理方法. 准备 import time,datetime 常用操作 输出当前的日期时间 方式一: now = time.localtime() print ' ...

  4. ABP官方文档翻译 1.5 多租户

    多租户 什么是多租户? 数据库和部署架构 多部署-多数据库 单部署-多数据库 单部署-单数据库 单部署-混合数据库 多部署-单/多/混合数据库 ABP的多租户 启用多租户 租主和租户 会话 决定当前租 ...

  5. Java读者写者问题

    实验存档. 允许好几个人同时读,但是不允许在有人读的时候写,以及同一时间只能有一个人在写. 读者.java: package operating.entity.readerwriter; import ...

  6. ThinkPHP5执行流程分析

    1.入口文件(tp5\public\index.php) 作用: 1)定义目录常量. 2)加载框架引导目录. 2.框架引导目录(tp5\thinkphp\start.php) 作用: 1)引导基础文件 ...

  7. 20145211《网络渗透》Adobe阅读器渗透攻击

    20145211<网络渗透>Adobe阅读器渗透攻击 实验准备 1.用了一个kali,一个English Winxp3,并保证能相互ping通 2.开启显示隐藏文件 实验步骤: 1.开启m ...

  8. cogs 341:[NOI2005] 聪聪与可可

    ★★   输入文件:cchkk.in   输出文件:cchkk.out   简单对比 时间限制:1 s   内存限制:256 MB [问题描述] 在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小 ...

  9. LeetCode——Longest Repeating Character Replacement

    1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...

  10. 【cs231n】最优化笔记

    ): W = np.random.randn(10, 3073) * 0.0001 # generate random parameters loss = L(X_train, Y_train, W) ...