C#对 Dictionary进行排序 转
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进行排序 转的更多相关文章
- c# dictionary,list排序
Dictionary Key排序 Dictionary<string, string> dct= new Dictionary<string, string>(); Dicti ...
- Linq在Array,List,Dictionary中的应用
Linq在Array,List,Dictionary中的应用 今天在实际工作中需要对array,list,dictionary进行排序,试一试linq,发现非常好用,代码如下: using Syste ...
- 转:python dict按照value 排序
我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...
- (转)Python 字典排序
我们知道Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value ...
- C# 键值对排序
static void Main(string[] args) { SortedList sl = new SortedList(); sl.Add("001", "Za ...
- OrderBy排序和IComparer的使用
https://www.cnblogs.com/May-day/p/7490334.html 一,OrderBy排序在MDSN中有两种使用方法,如下 1>第一种方法的使用,就是根据某个字段排序, ...
- python dict sorted 排序
https://www.cnblogs.com/linyawen/archive/2012/03/15/2398292.html 我们知道Python的内置dictionary数据类型是无序的,通过k ...
- python的dict如何排序
Python的内置dictionary数据类型是无序的,通过key来获取对应的value.可是有时我们需要对dictionary中 的item进行排序输出,可能根据key,也可能根据value来排 # ...
- 2.排序检索数据 ---SQL
order by 一.排序数据 SELECT prod_name FROM Products ORDER BY prod_name; ORDER BY子句的位置 在指定一条ORDER BY子句时,应该 ...
随机推荐
- 【等待事件】序列等待事件总结(enq: SQ - contention、row cache lock、DFS lock handle和enq: SV - contention)
[等待事件]序列等待事件总结(enq: SQ - contention.row cache lock.DFS lock handle和enq: SV - contention) 1 BLOG文档结 ...
- x-forwarded-for的深度挖掘
转自:http://www.cnblogs.com/yihang/archive/2010/12/19/1910365.html 如今利用nginx做负载均衡的实例已经很多了,针对不同的应用场合,还有 ...
- 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”
摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...
- cocos2d-x图层以及显示对象的基本使用
LogoNode: #ifndef LogoNode_hpp #define LogoNode_hpp #include <stdio.h> #include "cocos2d. ...
- 2013年 蓝桥杯预赛 java 本科A 题目
1.标题: 世纪末的星期 曾有邪教称1999年12月31日是世界末日.当然该谣言已经不攻自破. 还有人称今后的某个世纪末的12月31日,如果是星期一则会.... 有趣的是,任何一个世纪末的年份的12月 ...
- 烂泥:ubuntu安装KVM虚拟机管理virt-manager
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 打算学习KVM的图形界面管理器virt-manager,但是virt-manager只有linux系统的,没有windows下的.所以只能使用linux ...
- 【VB超简单入门】一、写在前面
每本书的前面总得写点什么,到我这里也自然不能免俗,前言这东西“存在即合理”,所以就随便写一点咯~ 首先这本书是给从未接触过编程的童鞋准备的,由于我学识疏浅,对VB也只是一知半解所以也只能讲一点点最基础 ...
- sphinx增量索引
首先建立一个计数表,保存数据表的最新记录ID CREATE TABLE `sph_counter` ( `id` int(11) unsigned NOT NULL, `max_id` int(1 ...
- C++类所占内存大小计算
C++类所占内存大小计算 说明:笔者的操作系统是32位的. class A {}; sizeof( A ) = ? sizeof( A ) = 1明明是空类,为什么编译器说它是1呢? 空类同样可以实例 ...
- php redis 获取指定前缀的所有key
php redis 获取指定前缀的所有key 以laravel框架为例: $key = $this->redis->keys('db:shipping:shippingId:' . &qu ...