C# .net 3.5 以上的版本引入 Linq 后,字典Dictionary排序变得十分简单,用一句类似 sql 数据库查询语句即可搞定;不过,.net 2.0 排序要稍微麻烦一点,为便于使用,将总结 .net 3.5 和 2.0 的排序方法。

  一、创建字典Dictionary 对象

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

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

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

  1、dictionary按值value排序

  private void DictonarySort(Dictionary<string, int> dic)
  {
    var dicSort = from objDic in dic orderby objDic.Value descending select objDic;
    foreach(KeyValuePair<string, int> kvp in dicSort)
      Response.Write(kvp.Key + ":" + kvp.Value + "<br />");
  }

  排序结果:

  index.html:50
  online.aspx:22
  news.aspx:18
  product.html:13
  aboutus.html:4

  上述代码是按降序(倒序)排列,如果想按升序(顺序)排列,只需要把变量 dicSort 右边的 descending 去掉即可。

  2、C# dictionary key 排序

  如果要按 Key 排序,只需要把变量 dicSort 右边的 objDic.Value 改为 objDic.Key 即可。

  三、.net 2.0 版本 Dictionary排序

  1、dictionary按值value排序(倒序)

  private void DictionarySort(Dictionary<string, int> dic)
  {
    if (dic.Count > 0)
    {
      List<KeyValuePair<string, int>> lst = new List<KeyValuePair<string, int>>(dic);
      lst.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
      {
        return s2.Value.CompareTo(s1.Value);
      });
      dic.Clear();

      foreach (KeyValuePair<string, int> kvp in lst)
        Response.Write(kvp.Key + ":" + kvp.Value + "<br />");
    }
  }

  排序结果:

  index.html:50
  online.aspx:22
  news.aspx:18
  product.html:13
  aboutus.html:4

  顺序排列:只需要把变量 return s2.Value.CompareTo(s1.Value); 改为 return s1.Value.CompareTo(s2.Value); 即可。

  2、C# dictionary key 排序(倒序、顺序)

  如果要按 Key 排序,倒序只需把 return s2.Value.CompareTo(s1.Value); 改为 return s2.Key.CompareTo(s1.Key);;顺序只需把return s2.Key.CompareTo(s1.Key); 改为 return s1.Key.CompareTo(s2.Key); 即可。

C#对 Dictionary进行排序 转的更多相关文章

  1. c# dictionary,list排序

    Dictionary Key排序 Dictionary<string, string> dct= new Dictionary<string, string>(); Dicti ...

  2. Linq在Array,List,Dictionary中的应用

    Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...

  3. 转:python dict按照value 排序

    我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...

  4. (转)Python 字典排序

    我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...

  5. C# 键值对排序

    static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Za ...

  6. OrderBy排序和IComparer的使用

    https://www.cnblogs.com/May-day/p/7490334.html 一,OrderBy排序在MDSN中有两种使用方法,如下 1>第一种方法的使用,就是根据某个字段排序, ...

  7. python dict sorted 排序

    https://www.cnblogs.com/linyawen/archive/2012/03/15/2398292.html 我们知道Python的内置dictionary数据类型是无序的,通过k ...

  8. python的dict如何排序

    Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value来排 # ...

  9. 2.排序检索数据 ---SQL

    order by 一.排序数据 SELECT prod_name FROM Products ORDER BY prod_name; ORDER BY子句的位置 在指定一条ORDER BY子句时,应该 ...

随机推荐

  1. (android) SharedPreferences 两种方式的存储范围

    1 SharedPreferences settings =Activity.getPreferences(Activity.MODE_PRIVATE); 访问数据的范围为 当前的activity 2 ...

  2. Android精品开源整理

    一.兼容类库 ActionBarSherlock : Action Bar是Android 3.0后才开始支持的,ActionBarSherlock是让Action Bar功能支持2.X后的所有平台, ...

  3. Java中关于WeakReference和WeakHashMap的理解

    新美大的10月11日的笔试中有一道选择题,让选择函数返回结果,代码如下: private static String test(){ String a = new String("a&quo ...

  4. nodejs创建一个HTTP服务器 简单入门级

    const http = require('http');//请求http.createServer(function(request, response){    /*createServer该函数 ...

  5. PHP模拟发送POST请求之三、用Telnet和fsockopen()模拟发送POST信息

    了解完了HTTP头信息和URL信息的具体内容,我们开始尝试自己动手写一段头信息发送到服务器.Windows内置命令Telnet可以帮助我们发送简单的HTTP请求. 并且TELNET是一个特别灵活的工具 ...

  6. C#连接MySql数据库的方法

    1.要连接MySql数据库必须首先下载MySql的连接.net的文件, 文件下载地址为http://download.csdn.net/detail/xiaoliu123586/91455792.解压 ...

  7. 20150912华为机考1之"输入一个字符串,将其中出现次数最多的字符输出"

    不吐槽华为的服务器了,直接上正文 输入:字符串(英文字母),长度不超过128 输出:出现频率最高的字母 思路写在注释文档 /* Input a string * Output the most fre ...

  8. 使用DateLocaleConverter和SimpleDateFormat实现字符串转换成日期

    使用DateLocaleConverter: public static void change() { String birthday = "1990-12-32"; DateL ...

  9. 如何实现ZBrush中部分模型的选择和隐藏

    在ZBrush中制作雕刻比较庞大细节又很丰富模型的时候,有时你可能只想显示模型的某些部分,有些部分挡住了视线想要暂时隐藏.ZBrush®软件中有一个选项功能使这项操作变得相当简单,像其他功能一样,使用 ...

  10. C# 使用NLog记录日志

    NLog是一个记录日志组件,和log4net一样被广泛使用,它可以将日志保存到文本文件.CSV.控制台.VS调试窗口.数据库等.最近刚用到这个组件,觉得不错,水一篇. 下载 通过Nuget安装NLog ...