C# 与vb.net 的Dictionary(字典)的键、值排序

      项目中可能须要用到Dictionary 排序,因而先作了一个小demo ,网上搜索真的没有能知足我须要的,都是相似的,因而理解改造,一上午就在查找,实践中过去了。如今把它实现了,把代码贴出来,算是一个笔记吧。但愿给须要的人也一个参考。html

1、C# 版本spa

代码.net

 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]));
            }

        }

效果图:code

2、vb.net版本orm

代码htm

 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

 效果图:blog

C#的理解好一些,vb.net的有点难度,花了很多时间。排序

参考 :http://www.cnblogs.com/sekihin/archive/2008/08/27/1277605.htmlget

相关文章
相关标签/搜索