Dictionary根据value值排序

//C#2.0 (VS2005)实现方法:
 
private void dicSort(ref Dictionary<string, int> dic)
        {
            List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dic);

            myList.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
            {
                return s2.Value.CompareTo(s1.Value);
            });
            dic.Clear();

            foreach (KeyValuePair<string, int> pair in myList)
            {
                dic.Add(pair.Key, pair.Value);
            }
        }


C#3.0 Lambda表达式 (VS2008)的实现方法:

  var list = dic.OrderBy(s => s.Value);

C#3.0 Linq (VS2008)的实现方法:

var dicSort = from d in dic

                          orderby d.Value

                          ascending

                          select d;

https://www.cnblogs.com/yc1224/p/13743697.html

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页