C#中Dictionary<TKey,TValue>排序方式的实现
作者:浮海扬尘 发布时间:2021-07-13 10:59:10
标签:C#,Dictionary,排序
自定义类:
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);
结果截图:
降序排序:
Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
结果截图:
按照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);
结果截图:
混合排序:类似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;
结果截图:
来源:https://www.cnblogs.com/5696-an/p/5625142.html


猜你喜欢
- 本文实例为大家分享了Android Scroller的使用方法,供大家参考,具体内容如下1、scrollTo和ScrollByView类定义
- mybatis group by substr传参报错报异常### Cause: java.sql.SQLSyntaxErrorExcept
- 要获取Java中的当前时间戳:Timestamp timestamp = new Timestamp(System.currentTimeM
- RestTemplate简介Spring RestTemplate 是 Spring 提供的用于访问 Rest 服务的客户端,RestTem
- 一问道StringBuffer与StringBuilder的区别,张口就来StringBuffer是线程安全的,因为它相关方法都加了sync
- 本文实例讲述了Android实现Activity水平和垂直滚动条的方法。分享给大家供大家参考,具体如下:<ScrollView xml
- 1.为什么需要动态内存分配关于这个问题,我们先看看我们之前是如何开辟内存的。int val = 20;//在栈空间上开辟四个字节char a
- 客户端代码:/// <summary>/// 批量上传图片/// </summary>/// <param n
- 小编对微信开发颇感兴趣,查阅了网上相关文章进行整理,方便大家一起学习。1、注册帐号--填写服务器配置在https://mp.weixin.q
- 引言最近在工作中结合线程池使用 InheritableThreadLocal 出现了获取线程变量“错误&rdqu
- 最近因为工作的原因用到了西门子PLC,在使用过程中一直在思考上位机和PLC的通讯问题,后来上网查了一下,找到了一个专门针对S7开发的一个.n
- 一、概述RocketMQ主要提供了两种消费模式:集群消费以及广播消费。我们只需要在定义消费者的时候通过setMessageModel(Mes
- 简介本文用示例介绍java的Period的用法。Duration和Period说明Duration类通过秒和纳秒相结合来描述一个时间量,最高
- 使用IDEA编辑Web项目已经逐渐超过了使用eclipse的人数,但是IDEA对于pom.xml的执行也就是Maven方式导包支持并不是很完
- 前言 同源策略:判断是否是同源的,主要看这三点,协议,ip,端口。同源策略就是浏览器出于网站安全性的考虑,限制不同源之间的资源相互访问的一种
- ThymeleafThymeleaf是最近SpringBoot推荐支持的模板框架,官网在thymeleaf.org这里。我们为什么要用Thy
- 新建多国语言包要在android studio项目中新建多国语言包,有两种方式,一种是手动建,一种是用使用android studio辅助建
- 背景在最近的项目中,有一个需求是对一个很大的数据库进行查询,数据量大概在几千万条。但同时对查询速度的要求也比较高。这个数据库之前在没有使用P
- 大家好,因为近期做需求中遇到了文件上传这个东西,而且我这个还是跨服务去传输文件的所以我这边使用了httpclient和RestTemplat
- 本文实例讲述了Java使用JDBC连接postgresql数据库。分享给大家供大家参考,具体如下:package tool;import j