服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C#中Dictionary<TKey,TValue>排序方式的实现

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

2022-11-02 11:29浮海扬尘 C#

这篇文章主要介绍了C#中Dictionary<TKey,TValue>排序方式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

自定义类:

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

Dictionary<int,自定义类>

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

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

结果截图:

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

降序排序:

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

结果截图:

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

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

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

关键修改这句:

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

结果截图:

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

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

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

等同于linq语句:

?
1
2
3
4
5
var dic1_SortedByKey = from n in dic1
 
orderby n.Value.stuScore, n.Value.stuAge descending
 
select n;

结果截图:

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

到此这篇关于C#中Dictionary<TKey,TValue>排序方式的实现的文章就介绍到这了,更多相关C# Dictionary<TKey,TValue>排序内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/5696-an/p/5625142.html

延伸 · 阅读

精彩推荐