自定义类:

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

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

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

关键修改这句:

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

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. String or binary data would be truncated 解决办法

    原因: 一般出现这个问题是因为数据库中的某个字段的长度小,而插入数据大 解决: 找到相应字段,修改表结构,使表字段大小相同或大于要插入的数据

  2. IP地址的组成

    IP地址的组成:是因特网上,为了每一台主机分配的,由32位二进制数组成的唯一标识符,包括网络地址和主机地址两部分,网络地址标识一个物理网络,主机地址标识这个网络下的一台主机,其表示形式如下  IP地址 ...

  3. it转行了

    国庆节后毅然的辞职了,辞职的还发生一些不愉快的事情,原来希望好去好来却不慌而散.接着便开始找工作,有过工作经验,找工作是容易些,不像刚毕业那会什么企业都没人要.因为实在是对it没感觉,所以找的都是销售 ...

  4. 后台接收前台传入的json 数据

    引入JSONArray的类型为org.json而不是net.sf.json,笔者开始引入的是net.sf.json.JSONArray, 但JSONObject.fromObject(obj)时报错报 ...

  5. PHP 文件包含总结 include require 命名空间 autoload spl_autoload_register 读取文件路径

    总结: 1. include或require包含其他文件 使用./或者 ../,这里的当前路径和上一层路径,取决于运行脚本的路径,会存在如下问题. 在写PHP程序时,经常要用到include或requ ...

  6. 为更好地设计数据库,重新整理sql server数据类型

    我们在平常开发过程中,在设计数据的时候,经常碰到数据类型选择的问题,为了更快,更合适地选择正确的数据类型,所以在这里做个总结. 分类 sql server 数据类型 c# 数据类型 描述 应用场景 字 ...

  7. mysql与sqlserver之间的关系转换

    sqlserver中的数据类型与mysql之间的对应 --sqlserver = 只复制表结构 = 复制表结构和表数据 --mysql create table xx like xx; 只复制表结构 ...

  8. fuelphp 问题1

    fuelphp遇到的一些小问题,写下来以防止别人遇到相同的问题,写的比较随意 1.引用到orm\model 报错:class Model_Test extends Orm\Model 报错内容:Cla ...

  9. CF #296 (Div. 1) A. Glass Carving 线段树

    A. Glass Carving time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  10. leetcode6

    好吧,今天晚上赶项目确实是做不了三道题目了,最近项目在网络编程方面有些进步,学到了东西,有时间再积累下来,很深的体会就是,和别人一起写代码,虽然蛋疼但是比自己一个人写要好点,不过发现自己对链表和排序什 ...