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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM