//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;
Dictionary根据value值排序
最新推荐文章于 2021-09-07 15:44:49 发布