C# sorted dictionary by value, key or length of the key, with descend and ascend

Lionsure 2020-02-01 Original by the website

With the introduction of Linq in C# .net 3.5 and above, sorted dictionary becomes very simple, and it can be done with a query similar to the SQL database query; however, it is a bit more troublesome in .net 2.0. For ease of use, we will summarize the sorted methods for .net 3.5 and 2.0.

The sorted dictionary can be sorted in ascending or descending order in C#, but also in ascending order by value, then in descending order by key or vice versa, and in the order of key or values. It's very simple, just call the built-in method.

 

I, C# dictionary create

If a website page traffic is saved in the dictionary, the key is the webpage name, and the value corresponds to the number of times the webpage has been accessed. Because webpage visits are counted continuously, int cannot be used as the key, only the webpage name. The code for creating a Dictionary object and adding data is as follows:

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

 

II, C# sorted dictionary for .net 3.5 and above(i.e C# ordered dictionary)

1. Order by value

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

 

Sort results(reverse order):

index.htm: 50
       item.aspx: 22
       news.aspx: 18
       product.htm: 13
       aboutus.htm: 4

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

 

2. Order by key

If you want to sort by Key, you only need to change objDict.Value to the right of the variable dictSort to objDict.Key.

 

3. C# dictionary sorts by value first and then key

If you want to sort the value by ascending order first and then Key to sort it in descending order, the code is as follows:

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

Call:

var dictSort = DictionarySort(dict);

Sort results:

product.htm: 13
       news.aspx: 18
       item.aspx: 22
       index.htm: 50
       aboutus.htm: 4

 

4. C# dictionary sort by length of the key

If it is required to sort according to the length of the Key, the shortest first and the longest last, the code is as follows:

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

Call:

var dictSort = DictionarySort(dict);

Sort results:

index.htm: 50
       item.aspx: 22
       news.aspx: 18
       product.htm: 13
       aboutus.htm: 4

 

If long keys are the first and short are the last, just change the code to:

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

 

 

III, C# sorted dictionary for .net 2.0

1. Order by value(Reverse order)

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

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

Sort results:

index.htm: 50
       item.aspx: 22
       news.aspx: 18
       product.htm: 13
       aboutus.htm: 4

 

Ascend: Just change return kvp2.Value.CompareTo(kvp1.Value); to return kvp1.Value.CompareTo(kvp2.Value);

 

2. Order by key(Descend and Ascend)

If you want to sort by Key, descend: return kvp2.Value.CompareTo(kvp1.Value); to return kvp2.Key.CompareTo(kvp1.Key);. Ascend: change return kvp2.Key.CompareTo(kvp1.Key);to return kvp1.Key.CompareTo(kvp2.Key);.