zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    编译安装MongoDB C++ Driver (win8.1 vs2013)
    Convert Sorted Array to Binary Search Tree
    Sqrt(x) 牛顿迭代法
    Sublime Text 2 新建C++ build system
    Add Two Numbers
    Two Sum *
    从TCP协议的原理来谈谈rst复位攻击
    【转载】专访阿里陶辉:大规模分布式系统、高性能服务器设计经验分享
    一个低级Illegal instruction错误的定位--忽略编译期警告就得加倍偿还
    【转载】“惊群”,看看nginx是怎么解决它的
  • 原文地址:https://www.cnblogs.com/annabook/p/4497094.html
Copyright © 2011-2022 走看看