编程语言
首页 > 编程语言> > C#中Dictionary排序方式

C#中Dictionary排序方式

作者:互联网

 

 

转载自: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