C# Dictionary(字典)的键、值排序

Dictionary<string, string> dic1 = new Dictionary<string, string>();

dic1.Add("ddd","123");
dic1.Add("aaa", "123");
dic1.Add("ccc", "123");
dic1.Add("fff", "123");
dic1.Add("eee", "123");
dic1.Add("bbb", "123");
Dictionary<string, string> dic1Asc = dic1.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
Dictionary<string, string> dic1desc = dic1.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);

Dictionary<string, string> dic1Asc1
= (from d in dic1
orderby d.Key ascending
select d).ToDictionary(k => k.Key, v => v.Value);
Dictionary<string, string> dic1desc2
= (from d in dic1
orderby d.Key descending
select d).ToDictionary(k => k.Key, v => v.Value);


List<string> list = new List<string>();
list.Add("aaa");
list.Add("ddd");
list.Add("bbb");
list.Add("ccc");
list.Add("bbb");
var ascList = list.OrderBy(o => o);
var descList = list.OrderByDescending(o => o);

var ascList1 = (from l in list
orderby l ascending
select l).ToList();
var descList2 = (from l in list
orderby l descending
select l).ToList();
string str = "";
————————————————

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:https://blog.csdn.net/xxj_jing/article/details/8005683

posted @   yinghualeihenmei  阅读(407)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示