First, create a dictionary Dictionary object
If the Dictionary is a website page traffic, key is the page name, the value of the page is the number of visits, because of the page's access to the statistics, so can not use int as key, only with the page name, create Dictionary object and add 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);
Ii.. NET 2.0 version dictionary sorting
list<keyvaluepair<string, int>> lst = new list<keyvaluepair<string, int>> (DIC);
Flashback arrangement: Simply swap the variable s2 and S1 for an example: return S1.Value.CompareTo (S2. Value);
Sorting is now in order
Lst. Sort (Delegate (keyvaluepair<string, int> s1, keyvaluepair<string, int> S2) {return s2. Value.compareto (S1. Value); });
List<keyvaluepair<string,int>> list=new list<keyvaluepair<string, int>> (DIC);
List. Sort ((s1, s2) = {return S1.Value.CompareTo (S2. Value); });
Iii.. NET 3.5 or later dictionary sorting (that is, LINQ dictionary sorting)
Sort by using LINQ
var dicsort = from Objdic in dic by objdic.value descending select Objdic;
var dicnew = dic. (S1=>S1. Value);
The output is to be used with this output: foreach (keyvaluepair<string, int> kvp in Dicsort) {Response.Write (KVP). Key + ":" + kvp. Value + "<br/>");}
C # Dictionary dictionary sort (sequential, reverse)