Sorting method of Dictionary<TKey, TValue> in C#

custom class:

copy code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Dictionary sorting method in namespace CSharp
{
    [Serializable]
    public class CustmonizedClass
    {
        public string stuName { get; set; }

        public int stuAge { get; set; }

        public string stuSex  { get; set; }

        public double stuScore { get; set; }
       
    }
}
copy code


Dictionary<int, custom class>

Sort in ascending order (OrderBy) and descending order (OrderByDescending) according to the Key value of the Dictionary:

copy code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Dictionary sorting method in namespace CSharp
{
     public  class Program
    {
        static void Main(string[] args)
        {
            CustmonizedClass cn1 = new CustmonizedClass();
            cn1.stuName = "Zhang San";
            cn1.stuAge = 18;
            cn1.stuSex = "男";
            cn1.stuScore = 89.5;

            CustmonizedClass cn2 = new CustmonizedClass();
            cn2.stuName = "Li Si";
            cn2.stuAge = 19;
            cn2.stuSex = "男";
            cn2.stuScore = 88.5;


            CustmonizedClass cn3 = new CustmonizedClass();
            cn3.stuName = "Wang Wu";
            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);
            //The above dic1.Add() is deliberately out of order

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

Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
结果截图:

Sort descending:

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

Result screenshot:

 

Sort in ascending order (OrderBy) and descending order (OrderByDescending) according to an attribute of the Value value of Dictionary:

  View Code

The key to modify this sentence:

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

Result screenshot:

Mixed sorting: Similar to EXCEL, the first column is in ascending order, and then the third column is in ascending order...

copy code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

Dictionary sorting method in namespace CSharp
{
     public  class Program
    {
        static void Main(string[] args)
        {
            CustmonizedClass cn1 = new CustmonizedClass();
            cn1.stuName = "Zhang San";
            cn1.stuAge = 18;
            cn1.stuSex = "男";
            cn1.stuScore = 89.5;

            CustmonizedClass cn2 = new CustmonizedClass();
            cn2.stuName = "Li Si";
            cn2.stuAge = 19;
            cn2.stuSex = "男";
            cn2.stuScore = 88.5;


            CustmonizedClass cn3 = new CustmonizedClass();
            cn3.stuName = "Wang Wu";
            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);
            //The above dic1.Add() is deliberately out of order
            //Key ascending order
            //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(p=>p.Key).ToDictionary(p => p.Key, o => o.Value);
            //Key descending
            //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
            //The stuAge property in Value
            //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p => p.Key, o => o.Value);
            //Mixed sorting is equivalent to the following linq statement
            //Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);

            //linq statement
            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();                        
        }
    }
}
copy code

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

Equivalent to linq statement:

var dic1_SortedByKey = from n in dic1

                         orderby n.Value.stuScore, n.Value.stuAge descending

                         select n;

Result screenshot:


          

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324770587&siteId=291194637