日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#中DictionaryTKey,TValue排序方式

發布時間:2024/9/20 C# 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#中DictionaryTKey,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; }} }


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);
結果截圖:

降序排序:

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

結果截圖:

按照Dictionary的Value值的某個屬性 升序排序(OrderBy)、降序排序(OrderByDescending):

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 dic1orderby n.Value.stuScore, n.Value.stuAge descendingselect 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;

結果截圖:

總結

以上是生活随笔為你收集整理的C#中DictionaryTKey,TValue排序方式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。