当前位置:   article > 正文

C#字典Dictionary排序(顺序、倒序)_dictionary 排序

dictionary 排序

 

这里是针对.NET版本过低的排序方式,没怎么用过,记录一下;

创建字典Dictionary 对象

  假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网页被访问的次数,由于网页的访问次要不断的统计,所以不能用 int 作为 key,只能用网页名称,创建 Dictionary 对象及添加数据代码如下:

 

 

  1. Dictionary<string, int> dic = new Dictionary<string, int>();
  2.   dic.Add("index.html", 50);
  3.   dic.Add("product.html", 13);
  4.   dic.Add("aboutus.html", 4);
  5.   dic.Add("online.aspx", 22);
  6.   dic.Add("news.aspx", 18);

法一、.net 2.0 版本 Dictionary排序

 

 

  1. List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(dic);
  2.   //倒叙排列:只需要把变量s2 和 s1 互换就行了 例: return s1.Value.CompareTo(s2.Value);
  3.   //进行排序 目前是顺序
  4.       lst.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
  5.       {
  6.         return s2.Value.CompareTo(s1.Value);
  7.       });
  8.       dic.Clear();

 

 

 

法二、.net 3.5 以上版本 Dictionary排序(即 linq dictionary 排序)

  使用linq排序

var dicSort = from objDic in dic orderby objDic.Value descending select objDic;
  1. Dictionary<int, int> tempDict = new Dictionary<int, int>();
  2. var sortResult1 = from pair in tempDict orderby pair.Value descending select pair; //以字典Value值逆序排序
  3. var sortResult2 = from pair in tempDict orderby pair.Key descending select pair; //以字典Key值逆序排序
  4. var sortResult3 = from pair in tempDict orderby pair.Key ascending select pair; //以字典Key值顺序排序
  5. var sortResult4 = from pair in tempDict orderby pair.Value ascending select pair; //以字典Value值顺序排序
  •  需要注意的是,得到的排序结构sortResult1,2,3,4是一个迭代器 IOrderedEnumerable<> 了,不再是字典,如果这样写则是错误的
  1. Dictionary<int, int> sortResult1 = from pair in tempDict orderby pair.Value descending select pair; //以字典Value值逆序排序

 

  •  

    如果字典的key值或Value值是引用类型的,可以根据根据引用类型中的某个字段来排序:

  1.  public class Info
  2. {
  3. public int m_ID;
  4. }
  5. Dictionary<int, Info> tempDict = new Dictionary<int, Info>();
  6. var sortResult1 = from pair in tempDict orderby pair.Value.m_ID descending select pair; //以字典Value值的字段 m_ID 逆序排序
 

 

  输出要用这个输出:

  

  1. foreach(KeyValuePair<string, int> kvp in dicSort)
  2. {
  3.   Response.Write(kvp.Key + ":" + kvp.Value + "<br />");
  4. }

法三:

  1. Dictionary<string, string> dic1 = new Dictionary<string, string>(); dic1.Add("ddd","123");
  2. dic1.Add("aaa", "123");
  3. dic1.Add("ccc", "123");
  4. dic1.Add("fff", "123");
  5. dic1.Add("eee", "123");
  6. dic1.Add("bbb", "123");
  7. Dictionary<string, string> dic1Asc = dic1.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  8. Dictionary<string, string> dic1desc = dic1.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  9. Dictionary<string, string> dic1Asc1 = (from d in dic1 orderby d.Key ascending select d).ToDictionary(k => k.Key, v => v.Value);
  10. Dictionary<string, string> dic1desc2 = (from d in dic1 orderby d.Key descending select d).ToDictionary(k => k.Key, v => v.Value);
  11. List<string> list = new List<string>();
  12. list.Add("aaa");
  13. list.Add("ddd");
  14. list.Add("bbb");
  15. list.Add("ccc");
  16. list.Add("bbb");
  17. var ascList = list.OrderBy(o => o);
  18. var descList = list.OrderByDescending(o => o);
  19. var ascList1 = (from l in list orderby l ascending select l).ToList();
  20. var descList2 = (from l in list orderby l descending select l).ToList();
  21. string str = "";

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/84481
推荐阅读
  

闽ICP备14008679号