当前位置:   article > 正文

C#中Dictionary<TKey,TValue>排序方式_unity dictionary 排序

unity dictionary 排序

C#中Dictionary<TKey,TValue>排序方式

按照Dictionary的Key值 升序排序(OrderBy)、降序排序(OrderByDescending):

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CSharp中Dictionary排序方式
  7. {
  8. [Serializable]
  9. public class CustmonizedClass
  10. {
  11. public string stuName { get; set; }
  12. public int stuAge { get; set; }
  13. public string stuSex { get; set; }
  14. public double stuScore { get; set; }
  15. }
  16. }

Dictionary<int,自定义类>

按照Dictionary的Key值 升序排序(OrderBy)、降序排序(OrderByDescending):

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace CSharp中Dictionary排序方式
  7. {
  8. public class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. CustmonizedClass cn1 = new CustmonizedClass();
  13. cn1.stuName = "张三";
  14. cn1.stuAge = 18;
  15. cn1.stuSex = "男";
  16. cn1.stuScore = 89.5;
  17. CustmonizedClass cn2 = new CustmonizedClass();
  18. cn2.stuName = "李四";
  19. cn2.stuAge = 19;
  20. cn2.stuSex = "男";
  21. cn2.stuScore = 88.5;
  22. CustmonizedClass cn3 = new CustmonizedClass();
  23. cn3.stuName = "王五";
  24. cn3.stuAge = 17;
  25. cn3.stuSex = "女";
  26. cn3.stuScore = 89.5;
  27. Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
  28. dic1.Add(3, cn1);
  29. dic1.Add(1, cn2);
  30. dic1.Add(2, cn3);
  31. //上面dic1.Add()故意不按照顺序
  32. Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
  33. foreach (KeyValuePair<int, CustmonizedClass> item in dic1_SortedByKey)
  34. {
  35. Console.WriteLine("Key:{0} ; Value: name:{1}, age:{2}, sex:{3}, score:{4} ",
  36. item.Key,item.Value.stuName,item.Value.stuAge,item.Value.stuSex,item.Value.stuScore);
  37. }
  38. Console.ReadLine();
  39. }
  40. }
  41. }

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
结果截图:

降序排序:

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);

结果截图:

 

按照Dictionary的Value值的某个属性 升序排序(OrderBy)、降序排序(OrderByDescending):

 View Code

关键修改这句:

 Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

结果截图:

混合排序:类似EXCEL中先按第一列升序、再按第3列的升序……

 View Code

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

等同于linq语句:

var dic1_SortedByKey = from n in dic1

                         orderby n.Value.stuScore, n.Value.stuAge descending

                         select n;

结果截图:


          

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/84535
推荐阅读
相关标签
  

闽ICP备14008679号