博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
键值对
阅读量:6929 次
发布时间:2019-06-27

本文共 1170 字,大约阅读时间需要 3 分钟。

  1. /*使用排序字典,默认只支持升序 
  2. SortedDictionary<DateTime, String> dd = new SortedDictionary<DateTime, String>(); 
  3. DateTime dt = DateTime.Now; 
  4. dd.Add(dt, "bbb"); 
  5. dd.Add(dt.AddDays(-1), "ccc"); 
  6. dd.Add(dt.AddDays(1), "aaa"); 
  7. //可以借助List得到降序键或值 
  8. List<String> lst = new List<String>(dd.Values); 
  9. lst.Reverse(); 
  10. */  
  11.   
  12. /*使用Linq查询 
  13. Dictionary<DateTime, String> dd = new Dictionary<DateTime, String>(); 
  14. DateTime dt = DateTime.Now; 
  15. dd.Add(dt, "bbb"); 
  16. dd.Add(dt.AddDays(-1), "ccc"); 
  17. dd.Add(dt.AddDays(1), "aaa"); 
  18.  
  19. var result = from pair in dd orderby pair.Key descending select pair; 
  20. List<String> lst = new List<String>(); 
  21. foreach (var kv in result) 
  22.     lst.Add(kv.Value); 
  23. //或 
  24. Dictionary<DateTime, String> dd2 = result.ToDictionary(p=>p.Key, p=>p.Value); 
  25. */  
  26.   
  27. //使用扩展方法  
  28. Dictionary<DateTime, String> dd = new Dictionary<DateTime, String>();  
  29. DateTime dt = DateTime.Now;  
  30. dd.Add(dt, "bbb");  
  31. dd.Add(dt.AddDays(-1), "ccc");  
  32. dd.Add(dt.AddDays(1), "aaa");  
  33.   
  34. Dictionary<DateTime, String> dicAsc = dd.OrderBy(p=>p.Key).ToDictionary(p=>p.Key, p=>p.Value);  
  35. Dictionary<DateTime, String> dicDesc = dd.OrderByDescending(p=>p.Key).ToDictionary(p=>p.Key, p=>p.Value);  

转载于:https://www.cnblogs.com/zhangruisoldier/p/4329489.html

你可能感兴趣的文章
java split方法
查看>>
JSTL / Filter
查看>>
很好的验证码
查看>>
Swift进阶之路(一)——单例模式、属性传值、代理传值、闭包传值
查看>>
IPV4
查看>>
XML基础之Jdom和DOM4J解析(转)
查看>>
Ad Hoc Distributed Queries的启用与关闭
查看>>
安全的“野指针”
查看>>
EXCEL实战技巧与数据分析(一)基础应用
查看>>
使用NIFTI指令画nii图像
查看>>
利用缓存、Timer间隔时间发送微信的实例,很有用的例子
查看>>
CentOS7使用firewalld打开关闭防火墙与端口
查看>>
UIPageViewController
查看>>
在线UI设计
查看>>
2017最好的JavaScript框架、库和工具 — SitePoint
查看>>
Linux 集群
查看>>
R语言推特twitter转发可视化分析
查看>>
模仿也是提高,纯css小技巧实现头部进度条
查看>>
js 改变只读属性的值
查看>>
nodejs vue SyntaxError:Block-scoped declarations (let,const,function,class) not yet supported
查看>>