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

作者:浮海扬尘 时间:2021-07-13 10:59:10 

自定义类:


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


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();            
   }
 }
}

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

结果截图:

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

降序排序:


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


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();            
   }
 }
}

关键修改这句:


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

结果截图:

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

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


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();            
   }
 }
}

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;

结果截图:

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

来源:https://www.cnblogs.com/5696-an/p/5625142.html

标签:C#,Dictionary,排序
0
投稿

猜你喜欢

  • Android RecyclerView自定义上拉和下拉刷新效果

    2022-03-17 23:06:00
  • java struts2学习笔记之线程安全

    2022-08-07 00:13:07
  • Android登录注册功能 数据库SQLite验证

    2023-10-01 20:58:55
  • C# WinForm国际化实现的简单方法

    2023-08-07 04:29:34
  • C# 标准事件流实例代码

    2022-06-21 16:29:14
  • java9迁移注意问题总结

    2022-07-19 11:26:30
  • Autowired的注入过程源码解析

    2022-04-29 17:53:36
  • Android布局之表格布局TableLayout详解

    2022-09-08 08:08:06
  • Android 反射注解与动态代理综合使用详解

    2023-01-13 12:30:56
  • Java 图片与byte数组互相转换实例

    2023-06-24 03:28:39
  • JavaWeb实现用户登录与注册功能(服务器)

    2022-12-19 13:28:31
  • docker网络配置过程详解介绍

    2023-02-07 23:04:15
  • Android开发之开发者头条(二)实现左滑菜单

    2022-02-28 11:18:31
  • C#读写xml文件方法总结(超详细!)

    2023-11-23 13:16:40
  • MyBatis-Plus中最简单的查询操作教程(Lambda)

    2022-03-16 13:43:28
  • Android从源码的角度彻底理解事件分发机制的解析(下)

    2023-09-21 11:23:59
  • C#纹理画刷TextureBrush用法实例

    2023-03-17 07:23:12
  • Java基于Socket实现多人聊天室

    2022-11-08 14:11:12
  • Android详解之NoHttp最基本使用(无封装)

    2022-09-28 17:11:51
  • Android中JSON的4种解析方式使用和对比

    2023-05-03 11:32:10
  • asp之家 软件编程 m.aspxhome.com