• 程序開發 > C# > 正文

    C#字典Dictionary排序(順序、倒序、先 Value 再按 Key 排、按鍵長短排)

    亮術網 2020-02-01 本網原創

    C# .net 3.5 以上的版本引入 Linq 后,字典Dictionary排序變得十分簡單,用一句類似 sql 數據庫查詢語句即可搞定;不過,.net 2.0 排序要稍微麻煩一點,為便于使用,將總結 .net 3.5 和 2.0 的排序方法。

    在 C# 中,字典Dictionary排序既可按升序排又可按降序排,還可先按值升序排再按鍵降序排或反過來,并且還能按鍵或值的長短排;它們排序的代碼都很簡單,直接調內置的方法即可實現。

     

    一、創建字典Dictionary 對象

    假如 Dictionary 中保存的是一個網站頁面流量,key 是網頁名稱,值value對應的是網頁被訪問的次數,由于網頁的訪問次要不斷的統計,所以不能用 int 作為 key,只能用網頁名稱,創建 Dictionary 對象及添加數據代碼如下:

    Dictionary<string, int> dic = new Dictionary<string, int>();
      dic.Add("index.html", 50);
      dic.Add("product.html", 13);
      dic.Add("aboutus.html", 4);
      dic.Add("online.aspx", 22);
      dic.Add("news.aspx", 18);

     

    二、.net 3.5 以上版本 Dictionary排序(即 linq dictionary 排序)

    1、Dictionary按值value排序

    private void DictionarySort(Dictionary<string, int> dic)
      {
        var dicSort = from objDic in dic orderby objDic.Value descending select objDic;
        foreach(KeyValuePair<string, int> kvp in dicSort)
        Response.Write(kvp.Key + ":" + kvp.Value + "<br />");
      }

     

    排序結果(倒序):

    index.html:50
      online.aspx:22
      news.aspx:18
      product.html:13
      aboutus.html:4

    上述代碼是按降序(倒序)排列,如果想按升序(順序)排列,只需要把變量 dicSort 右邊的 descending 去掉即可。

     

    2、C# Dictionary key 排序

    如果要按 Key 排序,只需要把變量 dicSort 右邊的 objDic.Value 改為 objDic.Key 即可。

     

    3、C# Dictionary 先按值 Value 再按鍵 Key 排序

    假如要先按值 Value 升序排序再按鍵 Key 降序排序,代碼如下:

    private IOrderedEnumerable<KeyValuePair<string, int>> DictionarySort(Dictionary<string, int> dic)
      {
        return dic.OrderBy(i => i.Value).OrderByDescending(i => i.Key);
      }

    調用:

    var dicSort = DictionarySort(dic);

    排序結果:

    product.html:13
      online.aspx:22
      news.aspx:18
      index.html:50
      aboutus.html:4

     

    4、C# Dictionary 按鍵 Key 的長度排序

    假如要求根據鍵 Key 的長度排序,短的排在先、長的排在后,代碼如下:

    private IOrderedEnumerable<KeyValuePair<string, int>> DictionarySort(Dictionary<string, int> dic)
      {
        return dic.OrderBy(i => i.Key.Length);
      }

    調用:

    var dicSort = DictionarySort(dic);

    排序結果:

    news.aspx:18
      index.html:50
      online.aspx:22
      product.html:13
      aboutus.html:4

     

    假如要求長的排在先、短的排在后,只需把代碼改為:

    return dic.OrderByDescending(i => i.Key.Length);

     

     

    三、.net 2.0 版本 Dictionary排序

    1、dictionary按值value排序(倒序)

    private void DictionarySort(Dictionary<string, int> dic)
      {
        if (dic.Count > 0)
        {
          List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(dic);
          lst.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
          {
            return s2.Value.CompareTo(s1.Value);
          });
          dic.Clear();

        foreach (KeyValuePair<string, int> kvp in lst)
            Response.Write(kvp.Key + ":" + kvp.Value + "<br />");
        }
      }

    排序結果:

    index.html:50
      online.aspx:22
      news.aspx:18
      product.html:13
      aboutus.html:4

     

    順序排列:只需要把變量 return s2.Value.CompareTo(s1.Value); 改為 return s1.Value.CompareTo(s2.Value); 即可。

     

    2、C# dictionary key 排序(倒序、順序)

    如果要按 Key 排序,倒序只需把 return s2.Value.CompareTo(s1.Value); 改為 return s2.Key.CompareTo(s1.Key);;順序只需把return s2.Key.CompareTo(s1.Key); 改為 return s1.Key.CompareTo(s2.Key); 即可。

    本文濃縮標簽:Dictionary排序
    996热re视频精品视频这里