【转】Dictionary排序

C#2.0 (VS2005)实现方法: Dictionary<string,string> dic =newDictionary<string,string>(); dic.Add("2","c Item"); dic.Add("6","f Item"); dic.Add("1","a Item"); dic.Add("7","g Item"); dic.Add("3","b Item"); dic.Add("5","e Item"); dic.Add("4","d Item"); List<KeyValuePair<string,string>> myList =newList<KeyValuePair<string,string>>(dic); myList.Sort(delegate(KeyValuePair<string,string> s1,KeyValuePair<string,string> s2) { returns1.Value.CompareTo(s2.Value); }); dic.Clear(); foreach(KeyValuePair<string,string> pairinmyList) { dic.Add(pair.Key, pair.Value); } foreach(stringkeyindic.Keys) { Response.Write(dic[key] +"<br />"); } C#3.0 Lambda表达式 (VS2008)的实现方法: Dictionary<string,string> dic =newDictionary<string,string>(); dic.Add("2","c Item"); dic.Add("6","f Item"); dic.Add("1","a Item"); dic.Add("7","g Item"); dic.Add("3","b Item"); dic.Add("5","e Item"); dic.Add("4","d Item"); varlist = dic.OrderBy(s => s.Value); foreach(varsinlist) { Response.Write(s.Value +"<br />"); } C#3.0 Linq (VS2008)的实现方法: Dictionary<string,string> dic =newDictionary<string,string>(); dic.Add("2","c Item"); dic.Add("6","f Item"); dic.Add("1","a Item"); dic.Add("7","g Item"); dic.Add("3","b Item"); dic.Add("5","e Item"); dic.Add("4","d Item"); vardicSort =fromdindic orderbyd.Value ascending selectd; foreach(stringkeyindic.Keys) { Response.Write(dic[key] +"<br />"); }
posted @ 2012-07-16 20:46  adodo1  Views(99)  Comments(0Edit  收藏  举报