• 【转】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 />"); }
  • 相关阅读:
    python小程序之购物系统
    列表,元祖,字典的使用
    几个python小程序
    default
    RTTI
    man
    养喜神去杀机
    IDEA+Maven+Git
    入门
    CheckStyle简介
  • 原文地址:https://www.cnblogs.com/adodo1/p/4328086.html
Copyright © 2020-2023  润新知