自定义类:

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 = ;
cn1.stuSex = "男";
cn1.stuScore = 89.5; CustmonizedClass cn2 = new CustmonizedClass();
cn2.stuName = "李四";
cn2.stuAge = ;
cn2.stuSex = "男";
cn2.stuScore = 88.5; CustmonizedClass cn3 = new CustmonizedClass();
cn3.stuName = "王五";
cn3.stuAge = ;
cn3.stuSex = "女";
cn3.stuScore = 89.5; Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
dic1.Add(, cn1);
dic1.Add(, cn2);
dic1.Add(, 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 = ;
cn1.stuSex = "男";
cn1.stuScore = 89.5; CustmonizedClass cn2 = new CustmonizedClass();
cn2.stuName = "李四";
cn2.stuAge = ;
cn2.stuSex = "男";
cn2.stuScore = 88.5; CustmonizedClass cn3 = new CustmonizedClass();
cn3.stuName = "王五";
cn3.stuAge = ;
cn3.stuSex = "女";
cn3.stuScore = 89.5; Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
dic1.Add(, cn1);
dic1.Add(, cn2);
dic1.Add(, 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 = ;
cn1.stuSex = "男";
cn1.stuScore = 89.5; CustmonizedClass cn2 = new CustmonizedClass();
cn2.stuName = "李四";
cn2.stuAge = ;
cn2.stuSex = "男";
cn2.stuScore = 88.5; CustmonizedClass cn3 = new CustmonizedClass();
cn3.stuName = "王五";
cn3.stuAge = ;
cn3.stuSex = "女";
cn3.stuScore = 89.5; Dictionary<int, CustmonizedClass> dic1 = new Dictionary<int, CustmonizedClass>();
dic1.Add(, cn1);
dic1.Add(, cn2);
dic1.Add(, 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>排序方式的更多相关文章

  1. C#中Dictionary排序方式

    转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...

  2. discuz 修改亮剑积分商城2.91模板(在常用设置中添加商场首页排序方式的背景颜色)

    在应用 -> 积分商城 -> 常用设置 中添加 商场首页排序方式 的背景颜色修改功能 步骤: 1.找到并打开此页面对应的模板source\plugin\aljsc\template\set ...

  3. 使用jdk中提供的排序方式

    package com.bjpowernode.t01; import java.util.Arrays; /** * 使用jdk中提供的排序方式 * */public class TestArray ...

  4. OBjective-C:在可变数组NSMutableArray中添加相同对象后,进行自定义的排序方式输出

    以下为自定义的排序方式的实现 #import "Person+Compare.h" @implementation Person (Compare) -(NSComparisonR ...

  5. C#字典Dictionary排序(顺序、倒序)

    这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...

  6. 浅析SQL查询语句未显式指定排序方式,无法保证同样的查询每次排序结果都一致的原因

    本文出处:http://www.cnblogs.com/wy123/p/6189100.html 标题有点拗口,来源于一个开发人员遇到的实际问题 先抛出问题:一个查询没有明确指定排序方式,那么,第二次 ...

  7. WPF中资源引用方式汇总

    在WPF应用程序开发中,总是难以记住各种访问资源的方法,遂逐一记下. 先从资源是否编译到程序集分类 一.程序集资源 资源在编译的时候嵌入到程序集中.WPF中的XAML会被编译为BAML,图片等其他资源 ...

  8. java中的排序

    排序是数据结构中重要的一个部分,也是在实际开发中最易遇到的问题之一,当然了,你也可以不考虑这些排序的算法,直接把要排序的数据insert到数据库中,用数据库的order by再select一下,也能产 ...

  9. [MySQL] 字符集和排序方式

    字符串类型 MySQL的字符串分为两大类: 1)二进制字符串:即一串字节序列,对字节的解释不涉及字符集,因此它没有字符集和排序方式的概念 2)非二进制字符串:由字符构成的序列,字符集用来解释字符串的内 ...

随机推荐

  1. Linux 利用 locate 和 find 查找文件

    Linux 利用 locate 和 find 查找文件 命令 locate 用于快速查找文件.文件夹.此命令并没有在磁盘上查找所有文件,而是在预先建立的数据库里进行搜索.可以使用 updatedb 命 ...

  2. JAVA06数组之动手动脑问题解决

     一.随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中. 1.设计思路:首先生成10个随机数,然后存放至长度至少是10的数组中,然后计算10个随机 ...

  3. OpenBSD内核之引导MBR

    MBR的介绍网上很多,没错,就那个最后以0x55AA结尾的512字节的引导块,OpenBSD提供了引导MBR实现:OpenBSD在x86上的引导过程为MBR --> PBR --> boo ...

  4. jquery 跨域访问问题 转

    http://zld406504302.iteye.com/blog/1677937 服务器,jsp <%@ page language="java" contentType ...

  5. [转]用CSS给SVG <use>的内容添加样式

    来源:http://www.w3cplus.com/svg/styling-svg-use-content-css.html?utm_source=tuicool&utm_medium=ref ...

  6. b/s 猫大叔

       好复杂,要把客户端文件传到服务端,再从服务端传到FTP......肿么设计的这是....       至少你都要把客户端的文件读到服务端的内存中,因为B/S控制器在S端,不在B端 非要用B/S实 ...

  7. ecshop后台新功能及权限的添加

    1.在后台"推荐管理"里添加"推荐人分成"."会员分成"两个操作功能以及权限 index.php?act=menu incluedes/in ...

  8. lock

    #ifndef lock_h #define lock_h #include <stdint.h> #include <string.h> #include "myd ...

  9. [CentOS7服务器] 更改系统时间

    未知何故,服务器上的系统时间不对,比实际的UTC快了将近63分钟.在涉及本地文件与远程服务器文件的时间戳校验时,容易产生混淆. 这里把系统时间更正的过程记录如下. 参考资料:http://www.ce ...

  10. QT 做软件盘

    最近搞了一个组织细胞脱水机项目,当然,对于国内的项目都是仿来仿去的,我们也不例外,开启被仿机器后,第一个看到的界面就是用户登录界面,需要输入中文,作为一个程序员,我的第一反应就是我需要采用什么用的框架 ...