C#中Dictionary排序方式的实现
自定义类:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceCSharp中Dictionary排序方式 { [Serializable] publicclassCustmonizedClass { publicstringstuName{get;set;} publicintstuAge{get;set;} publicstringstuSex{get;set;} publicdoublestuScore{get;set;} } }
Dictionary
按照Dictionary的Key值升序排序(OrderBy)、降序排序(OrderByDescending):
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceCSharp中Dictionary排序方式 { publicclassProgram { staticvoidMain(string[]args) { CustmonizedClasscn1=newCustmonizedClass(); cn1.stuName="张三"; cn1.stuAge=18; cn1.stuSex="男"; cn1.stuScore=89.5; CustmonizedClasscn2=newCustmonizedClass(); cn2.stuName="李四"; cn2.stuAge=19; cn2.stuSex="男"; cn2.stuScore=88.5; CustmonizedClasscn3=newCustmonizedClass(); cn3.stuName="王五"; cn3.stuAge=17; cn3.stuSex="女"; cn3.stuScore=89.5; Dictionarydic1=newDictionary (); dic1.Add(3,cn1); dic1.Add(1,cn2); dic1.Add(2,cn3); //上面dic1.Add()故意不按照顺序 Dictionary dic1_SortedByKey=dic1.OrderBy(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value); foreach(KeyValuePair itemindic1_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(); } } }
Dictionarydic1_SortedByKey=dic1.OrderBy(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value);
结果截图:
降序排序:
Dictionarydic1_SortedByKey=dic1.OrderByDescending(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value);
结果截图:
按照Dictionary的Value值的某个属性升序排序(OrderBy)、降序排序(OrderByDescending):
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceCSharp中Dictionary排序方式 { publicclassProgram { staticvoidMain(string[]args) { CustmonizedClasscn1=newCustmonizedClass(); cn1.stuName="张三"; cn1.stuAge=18; cn1.stuSex="男"; cn1.stuScore=89.5; CustmonizedClasscn2=newCustmonizedClass(); cn2.stuName="李四"; cn2.stuAge=19; cn2.stuSex="男"; cn2.stuScore=88.5; CustmonizedClasscn3=newCustmonizedClass(); cn3.stuName="王五"; cn3.stuAge=17; cn3.stuSex="女"; cn3.stuScore=89.5; Dictionarydic1=newDictionary (); dic1.Add(3,cn1); dic1.Add(1,cn2); dic1.Add(2,cn3); //上面dic1.Add()故意不按照顺序 //Key升序 //Dictionary dic1_SortedByKey=dic1.OrderBy(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value); //Key降序 //Dictionary dic1_SortedByKey=dic1.OrderByDescending(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value); //Value中stuAge属性 Dictionary dic1_SortedByKey=dic1.OrderBy(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value); foreach(KeyValuePair itemindic1_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(); } } }
关键修改这句:
Dictionarydic1_SortedByKey=dic1.OrderBy(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);
结果截图:
混合排序:类似EXCEL中先按第一列升序、再按第3列的升序……
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingSystem.Text; usingSystem.Threading.Tasks; namespaceCSharp中Dictionary排序方式 { publicclassProgram { staticvoidMain(string[]args) { CustmonizedClasscn1=newCustmonizedClass(); cn1.stuName="张三"; cn1.stuAge=18; cn1.stuSex="男"; cn1.stuScore=89.5; CustmonizedClasscn2=newCustmonizedClass(); cn2.stuName="李四"; cn2.stuAge=19; cn2.stuSex="男"; cn2.stuScore=88.5; CustmonizedClasscn3=newCustmonizedClass(); cn3.stuName="王五"; cn3.stuAge=17; cn3.stuSex="女"; cn3.stuScore=89.5; Dictionarydic1=newDictionary (); dic1.Add(3,cn1); dic1.Add(1,cn2); dic1.Add(2,cn3); //上面dic1.Add()故意不按照顺序 //Key升序 //Dictionary dic1_SortedByKey=dic1.OrderBy(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value); //Key降序 //Dictionary dic1_SortedByKey=dic1.OrderByDescending(p=>p.Key).ToDictionary(p=>p.Key,o=>o.Value); //Value中stuAge属性 //Dictionary dic1_SortedByKey=dic1.OrderBy(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value); //混合排序等同于下列的linq语句 //Dictionary dic1_SortedByKey=dic1.OrderBy(o=>o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value); //linq语句 vardic1_SortedByKey=fromnindic1 orderbyn.Value.stuScore,n.Value.stuAgedescending selectn; foreach(KeyValuePair itemindic1_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(); } } }
Dictionarydic1_SortedByKey=dic1.OrderBy(o=>o.Value.stuScore).ThenByDescending(o=>o.Value.stuAge).ToDictionary(p=>p.Key,o=>o.Value);
等同于linq语句:
vardic1_SortedByKey=fromnindic1 orderbyn.Value.stuScore,n.Value.stuAgedescending selectn;
结果截图:
到此这篇关于C#中Dictionary
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。