当前位置:   article > 正文

Dictionary根据value值排序_value 排序dic c#

value 排序dic c#
  1. //C#2.0 (VS2005)实现方法:
  2. private void dicSort(ref Dictionary<string, int> dic)
  3. {
  4. List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dic);
  5. myList.Sort(delegate (KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
  6. {
  7. return s2.Value.CompareTo(s1.Value);
  8. });
  9. dic.Clear();
  10. foreach (KeyValuePair<string, int> pair in myList)
  11. {
  12. dic.Add(pair.Key, pair.Value);
  13. }
  14. }
  15. C#3.0 Lambda表达式 (VS2008)的实现方法:
  16. var list = dic.OrderBy(s => s.Value);
  17. C#3.0 Linq (VS2008)的实现方法:
  18. var dicSort = from d in dic
  19. orderby d.Value
  20. ascending
  21. select d;

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号