ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C#中Dictionary排序方式

2022-06-09 11:34:39  阅读:167  来源: 互联网

标签:stuAge Dictionary C# Value dic1 Key using 排序


 

 

转载自:https://www.cnblogs.com/5696-an/p/5625142.html

自定义类:

 1 https://files.cnblogs.com/files/xunhanliu/d3.v4.js
 2 
 3 using System;
 4 using System.Collections.Generic;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 
 9 namespace CSharp中Dictionary排序方式
10 {
11     [Serializable]
12     public class CustmonizedClass
13     {
14         public string stuName { get; set; }
15 
16         public int stuAge { get; set; }
17 
18         public string stuSex  { get; set; }
19 
20         public double stuScore { get; set; }
21        
22     }
23 }

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

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):

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

关键修改这句:

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

结果截图:

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

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace CSharp中Dictionary排序方式
 8 {
 9      public  class Program
10     {
11         static void Main(string[] args)
12         {
13             CustmonizedClass cn1 = new CustmonizedClass();
14             cn1.stuName = "张三";
15             cn1.stuAge = 18;
16             cn1.stuSex = "男";
17             cn1.stuScore = 89.5;
18 
19             CustmonizedClass cn2 = new CustmonizedClass();
20             cn2.stuName = "李四";
21             cn2.stuAge = 19;
22             cn2.stuSex = "男";
23             cn2.stuScore = 88.5;
24 
25 
26             CustmonizedClass cn3 = new CustmonizedClass();
27             cn3.stuName = "王五";
28             cn3.stuAge = 17;
29             cn3.stuSex = "女";
30             cn3.stuScore = 89.5;
31 
32             Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
33             dic1.Add(3, cn1);
34             dic1.Add(1, cn2);
35             dic1.Add(2, cn3);
36             //上面dic1.Add()故意不按照顺序
37             //Key升序
38             //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
39             //Key降序
40             //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
41             //Value中stuAge属性
42             //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p => p.Key, o => o.Value);
43             //混合排序 等同于下列的linq语句
44             //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);
45 
46             //linq语句
47             var dic1_SortedByKey = from n in dic1
48 
49                          orderby n.Value.stuScore, n.Value.stuAge descending
50 
51                          select n;
52 
53             foreach (KeyValuePair<int, CustmonizedClass> item in dic1_SortedByKey) 
54             {
55                 Console.WriteLine("Key:{0} ; Value: name:{1}, age:{2}, sex:{3}, score:{4} ",
56                     item.Key,item.Value.stuName,item.Value.stuAge,item.Value.stuSex,item.Value.stuScore);
57             }
58             Console.ReadLine();                        
59         }
60     }
61 }

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;

结果截图:

标签:stuAge,Dictionary,C#,Value,dic1,Key,using,排序
来源: https://www.cnblogs.com/feifeifeisir/p/16358641.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有