C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn)
自定義類:
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);
結(jié)果截圖:

降序排序:
Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderByDescending(p => p.Key).ToDictionary(p => p.Key, o => o.Value);
結(jié)果截圖:

按照Dictionary的Value值的某個(gè)屬性 升序排序(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();
}
}
}
關(guān)鍵修改這句:
Dictionary<int, CustmonizedClass> dic1_SortedByKey = dic1.OrderBy(o => o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);
結(jié)果截圖:

混合排序:類似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;
結(jié)果截圖:

到此這篇關(guān)于C#中Dictionary<TKey,TValue>排序方式的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# Dictionary<TKey,TValue>排序內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C# ArrayList、HashSet、HashTable、List、Dictionary的區(qū)別詳解
- C#中數(shù)組、ArrayList、List、Dictionary的用法與區(qū)別淺析(存取數(shù)據(jù))
- C#數(shù)組中List, Dictionary的相互轉(zhuǎn)換問題
- C#常見的幾種集合 ArrayList,Hashtable,List<T>,Dictionary<K,V> 遍歷方法對(duì)比
- C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
- C# 解決在Dictionary中使用枚舉的效率問題
- C#字典Dictionary的用法說明(注重性能版)
- c# 用Dictionary實(shí)現(xiàn)日志數(shù)據(jù)批量插入
- 聊聊C# 中HashTable與Dictionary的區(qū)別說明
- c# List和Dictionary常用的操作
相關(guān)文章
C#控制臺(tái)帶參數(shù)程序源碼編寫實(shí)例講解
像ipconfig /all 這樣的CMD命令想必大家都知道,但是很多童鞋可能不知道怎么寫這樣的控制臺(tái)帶參數(shù)的程序,需要的朋友可以了解下2012-12-12
C#中靜態(tài)構(gòu)造函數(shù)的幾點(diǎn)說明介紹
本篇文章主要是對(duì)C#中靜態(tài)構(gòu)造函數(shù)的幾點(diǎn)說明進(jìn)行了介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
C# Color.FromArgb()及系統(tǒng)顏色對(duì)照表一覽
這篇文章主要介紹了C# Color.FromArgb()及系統(tǒng)顏色對(duì)照表一覽,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-01-01
C#?wpf?通過HwndHost渲染視頻的實(shí)現(xiàn)方法
日常開發(fā)中,特別是音視頻開發(fā),需要在界面上渲染視頻,比如制作一個(gè)播放器、或者視頻編輯工具、以及視頻會(huì)議客戶端。通常拿到的是像素格式數(shù)據(jù),此時(shí)需要渲染到wpf窗口上就需要一定的方法,本文介紹一種通過hwnd渲染的方法,控件既能提供hwnd又能嵌入wpf窗口里2021-11-11
利用WCF雙工模式實(shí)現(xiàn)即時(shí)通訊
這篇文章主要介紹了利用WCF雙工模式實(shí)現(xiàn)即時(shí)通訊的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
C#中IEnumerable接口介紹并實(shí)現(xiàn)自定義集合
這篇文章介紹了C#中IEnumerable接口并實(shí)現(xiàn)自定義集合,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
C#?使用PrintDocument類打印標(biāo)簽的方法
本文介紹打印機(jī)初步配置,以及實(shí)現(xiàn)方法,標(biāo)簽主要展示資產(chǎn)基本信息以及二維碼,對(duì)C#?使用PrintDocument類打印標(biāo)簽的詳細(xì)過程感興趣的朋友一起看看吧2022-04-04

