项目中可能需要用到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. UVALive - 4671 K-neighbor substrings (FFT+哈希)

    题意:海明距离的定义:两个相同长度的字符串中不同的字符数.现给出母串A和模式串B,求A中有多少与B海明距离<=k的不同子串 分析:将字符a视作1,b视作0.则A与B中都是a的位置乘积是1.现将B ...

  2. Optimal Marks SPOJ - OPTM (按位枚举-最小割)

    题意:给一张无向图,每个点有其点权,边(i,j)的cost是\(val_i\ XOR \ val_j\).现在只给出K个点的权值,求如何安排其余的点,使总花费最小. 分析:题目保证权值不超过32位整型 ...

  3. 【转】阿里巴巴技术专家杨晓明:基于Hadoop技术进行地理空间分析

    转自:http://www.csdn.net/article/2015-01-23/2823687-geographic-space-base-Hadoop [编者按]交通领域正产生着海量的车辆位置点 ...

  4. SiteMesh使用(2.4.2)

    SiteMesh是一个网页布局和修饰的框架.我理解的是在一个母版页上引入页面各个位置的信息,从而拼接成一个页面展示出来.它定义了一个过滤器,把页面统一加上头部和底部. 我的项目是在springmvc中 ...

  5. GZFramework错误(升级修改)日志

    sqlserver下事务中处理出现为初始化selectcommand的connection属性修改CommandDataBase中的PrepareCommand方法

  6. vi重要操作指令

    [Ctrl] + [f] 萤幕『向下』移动一页,相当于[Page Down]按键( 常用 ) [Ctrl] + [b] 萤幕『向上』移动一页,相当于[Page Up]按键( 常用 ) 0 或功能键[H ...

  7. 负载均衡技术在CDN中发挥着重要作用

    转载地址:http://www.qicaispace.com/gonggao/server/page01/info07.asp CDN是一个经策略性部署的整体系统,能够帮助用户解决分布式存储.负载均衡 ...

  8. ElasticSearch(一)ElasticSearch的应用场景及为什么要选择ElasticSearch?

    先了解一下数据的分类 结构化数据又可以称之为行数据,存储在数据库里,可以用二维表结构来逻辑表达实现的数据.其实就是可以能够用数据或者统一的结构加以表示的数据.比如在数据表存储商品的库存,可以用整型表示 ...

  9. win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)

    转自:http://blog.csdn.net/lhh31/article/details/51979293 1.软件版本 首先我先安装了 python 2.7 pip是  8.1.2 2.当我要安装 ...

  10. iPhone Screen FAQ

    Q: Why is it a blank window after openning the app and connect mobile device? A: Please make sure th ...