C#中Dictionary<TKey,TValue>排序方式的實現(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值的某個屬性 升序排序(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>排序方式的實現(xiàn)的文章就介紹到這了,更多相關(guān)C# Dictionary<TKey,TValue>排序內(nèi)容請搜索腳本之家以前的文章或繼續(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> 遍歷方法對比
- C#并發(fā)容器之ConcurrentDictionary與普通Dictionary帶鎖性能詳解
- C# 解決在Dictionary中使用枚舉的效率問題
- C#字典Dictionary的用法說明(注重性能版)
- c# 用Dictionary實現(xiàn)日志數(shù)據(jù)批量插入
- 聊聊C# 中HashTable與Dictionary的區(qū)別說明
- c# List和Dictionary常用的操作
相關(guān)文章
在多線程中調(diào)用winform窗體控件的實現(xiàn)方法
這篇文章主要介紹了在多線程中調(diào)用winform窗體控件的實現(xiàn)方法,需要的朋友可以參考下2014-08-08C#中Invoke和BeginInvoke區(qū)別小結(jié)
有時候,我們不得不跨線程調(diào)用主界面的控件來進(jìn)行操作,所以為了方便的解決問題,.net為我們提供了Invoke?與beginInvoke,那么Invoke和BeginInvoke區(qū)別在哪,本文就來詳細(xì)的介紹一下2023-08-08Unity幸運(yùn)轉(zhuǎn)盤實戰(zhàn)項目
這篇文章主要為大家詳細(xì)介紹了Unity幸運(yùn)轉(zhuǎn)盤實戰(zhàn)項目,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能
這篇文章介紹了C#中泛型容器Stack<T>的用法并實現(xiàn)”撤銷/重做”功能,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10