c# .net

C# dictionary Dictionary sorting (sequence, reverse order)

C# dictionary Dictionary sorting (sequence, reverse order), someone asked me to explain?

After the introduction of Linq in C# .net 3.5 and above, dictionary dictionary sorting becomes very simple, and it can be done with a query similar to sql database; however, .net 2.0 sorting is a bit more troublesome, for ease of use, it will summarize .net 3.5 And the sorting method of 2.0.

  First, create a dictionary Dictionary object

  If the dictionary is saved in a website page, the key is the name of the page, and the value value corresponds to the number of times the page is accessed. Since the access to the page is constantly counted, the int cannot be used as the key, and only the name of the page can be used. Create a Dictionary object and add the data code as follows:

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

 

  Second, the .net 3.5 version of the Dictionary sort (ie linq dictionary sort)

  1, dictionary is sorted by value value

    private void DictonarySort(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 />");
        }
 

  Sort results:

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

  The above code is arranged in descending order (reverse order). If you want to sort in ascending order (sequence), you only need to remove the descending of the variable dicSort to the right.

  2, C# dictionary key sort

  If you want to sort by Key, just change the objDic.Value to the right of the variable dicSort to objDic.Key.

  Third, the .net 2.0 version of the Dictionary sort

  1, dictionary sorted by value value (reverse order)

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 />");
            }
        }

 Sort results:

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

  Ordering: Just change the variable return s2.Value.CompareTo(s1.Value); to return s1.Value.CompareTo(s2.Value);

  2, C# dictionary key sort (reverse order, order)

  If you want to sort by Key, the reverse order just needs to change return s2.Value.CompareTo(s1.Value); to return s2.Key.CompareTo(s1.Key);; the order just needs to return s2.Key.CompareTo(s1. Key); Change to s1.Key.CompareTo(s2.Key);

Post your comments / questions