项目中可能需要用到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. html-4, form 表单 输入、传文件、单选、多选、下拉菜单、文本描述、重置、submit、按钮限制输入

    <!-- form HTTP协议 action:提交的服务器网址 method:get(默认)| post(应用:登录注册.上传文件) 页面中的a img link 默认是get请求 input ...

  2. GitHub上最热门的11款开源安全工具

    恶意软件分析.渗透测试.计算机取证——GitHub托管着一系列引人注目的安全工具.足以应对各类规模下计算环境的实际需求.  作为开源开发领域的基石,“所有漏洞皆属浅表”已经成为一条着名的原则甚至是信条 ...

  3. 分布式远程服务调用(RPC)框架

    分布式远程服务调用(RPC)框架 finagle:一个支持容错,协议无关的RPC系统 热门度(没变化) 10.0 活跃度(没变化) 10.0  Watchers:581 Star:6174 Fork: ...

  4. GIT使用—补丁与钩子

    一.补丁 生成补丁 [root@localhost buding]# echo B > file;git add file;git commit -m "B" [master ...

  5. Android模拟器Intel Atom下载安装配置

    https://software.intel.com 在Android x86模拟器Intel Atom x86 System Image时提示Intel execute disable bit(xd ...

  6. WCF用户名密码验证方式

    WCF使用用户名密码验证 服务契约 namespace WCFUserNameConstract { [ServiceContract] public interface IWcfContract { ...

  7. 设置 vadio 和checkbox是否选中

    1.js方案 <!DOCTYPE html> <html> <head> <meta charset=utf-8 />  <title>te ...

  8. Vue数据绑定失效

    首先,我们得明白Vue数据响应的原理: 以对象为例:当把一个JavaScript对象传给Vue实例的data选项时,Vue将遍历此对象所有的属性,并使用Object.defineProperty把这些 ...

  9. 20145322第四周JAVA程序设计基础学习总结

    20145322 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 一.继承共同行为 何谓继承 1 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那么多个类无需再 ...

  10. react native 0.56.0版本在windows下有bug不能正常运行

    react native的0.56.0版本在windows下有bug不能正常运行请init 0.55.4的版本 react-native init MyApp --version 0.55.4 注意v ...