zoukankan      html  css  js  c++  java
  • C#中Dictionary<TKey,TValue>排序方式

    自定义类:

    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; }
           
        }
    }
    View Code


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

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

    关键修改这句:

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

    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;

    结果截图:


              

  • 相关阅读:
    【剑指offer】二叉搜索树与双向链表
    【剑指offer】复杂链表的复制
    【剑指offer】二叉树中和为某一值的路径
    2018.12.30 Intellij IDEA设置main方法自动补全
    2018.12.29 Spring FrameWork各个版本下载
    2018.12.26 Mac下的Eclipse在编辑Spring配置文件xml时自动提示类class包名配置
    2018.12.25 Spring中JDBCTemplate模版API学习
    2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
    2018.12.22 Spring学习02
    2018.12.24 Ubuntu18.0.4 主题参考
  • 原文地址:https://www.cnblogs.com/5696-an/p/5625142.html
Copyright © 2011-2022 走看看