c# Dictionary的遍历和排序

c#遍历的两种方式 for和foreach

  for: 需要指定首位数据、末尾数据、数据长度; for遍历语句中可以改变数据的值; 遍历规则可以自定义,灵活性较高

  foreach: 需要实现ienumerator接口; 在遍历中不可以改变数据的值; 遍历规则只能是'++' ; 但查询效率较高

Dictionary遍历方式:

Dictionary<string, int> list = new Dictionary<string, int>();
list.Add("d", 1);
//3.0以上版本
foreach (var item in list)
{
Console.WriteLine(item.Key + item.Value);
} //KeyValuePair<T,K>
foreach (KeyValuePair<string, int> kv in list)
{
Console.WriteLine(kv.Key + kv.Value);
} //通过键的集合取
foreach (string key in list.Keys)
{
Console.WriteLine(key + list[key]);
} //直接取值
foreach (int val in list.Values)
{
Console.WriteLine(val);
} //非要采用for的方法也可
List<string> test = new List<string>(list.Keys);
for (int i = 0; i < list.Count; i++)
{
Console.WriteLine(test[i] + list[test[i]]);
}

List排序:

            Hashtable ht=new Hashtable(); 

            ht.Add("E","e");
ht.Add("A","a");
ht.Add("C","c");
ht.Add("B","b"); ArrayList lst=new ArrayList(ht.Keys);
lst.Sort();
foreach(string key in lst)
{
listBox1.Items.Add("key:" + key + " vlaue:"+ht[key]);
}

Dictionary排序

  排序思路:

  1>用一个List保存Dictionary的数据

  2>对新的List进行排序

  3>从List获取排序好的值,重新添加进Dictionary

 protected Dictionary<string, int> SortDictionary_Desc(Dictionary<string, int> dic)
{
List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dic);
myList.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
{
return s2.Value.CompareTo(s1.Value);
});
dic.Clear();
foreach (KeyValuePair<string, int> pair in myList)
{
dic.Add(pair.Key, pair.Value);
}
return dic;
} protected Dictionary<string, int> SortDictionary_Asc(Dictionary<string, int> dic)
{
List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dic);
myList.Sort(delegate(KeyValuePair<string, int> s1, KeyValuePair<string, int> s2)
{
return s1.Value.CompareTo(s2.Value);
});
dic.Clear();
foreach (KeyValuePair<string, int> pair in myList)
{
dic.Add(pair.Key, pair.Value);
}
return dic;
}

c# Dictionary的遍历和排序的更多相关文章

  1. c# Dictionary的遍历和排序(转)

    c#遍历的两种方式 for和foreach for: 需要指定首位数据.末尾数据.数据长度: for遍历语句中可以改变数据的值: 遍历规则可以自定义,灵活性较高 foreach: 需要实现ienume ...

  2. Object-c学习之路八(NSArray(数组)遍历和排序)

    今天学习了NSArray的遍历和排序,现在在这里做一下总结: 遍历现在实现了四中方法: 排序大概有三中方法:(代码中都有注释) 关于对象的排序还是以Student和Book为例 每个Student持有 ...

  3. jdk8中关于操作集合的一些新特性,遍历和排序操作

    jdk8增加了不少新的东西,在集合操作这块,就有如 lamda表达式,stream,sort,optional等新的类,主要涉及遍历和排序等方面,新特性提升了不少性能,我们开发就是要拥抱新事物,守着老 ...

  4. PCB 合拼遍历(全排序+旋转90度) 基本遍历方法

    分享一下PCB合拼的组合的遍历方法,在分享之前先纠正一下 PCB拼板之多款矩形排样算法实现--学习  时间复杂度计算错误  一.PCB 合拼(全排序+旋转90度)的时间复杂度是多少? 二.合拼遍历(全 ...

  5. java 集合之Arraylist的遍历及排序

    最近培训是先学习java基础 从最基本的开始学起 因为今天刚刚开博客 要把上周的一些重点内容归纳一下 1.Arraylist常用遍历以及排序 import java.util.ArrayList; i ...

  6. C# Dictionary 的几种遍历方法,排序

    Dictionary<string, int> list = new Dictionary<string, int>(); list.Add(); //3.0以上版本 fore ...

  7. C# Dictionary的遍历理解

    C# Dictionary容器类的理解 本文章由cartzhang编写,转载请注明出处. 所有权利保留. 文章链接:http://blog.csdn.net/cartzhang/article/det ...

  8. C#对Dictionary的按Value排序

    使用List对其进行排序 using System; using System.Collections.Generic; using System.Text; namespace ConsoleApp ...

  9. [java学习笔记]java语言基础概述之数组的定义&常见操作(遍历、排序、查找)&二维数组

    1.数组基础 1.什么是数组:           同一类型数据的集合,就是一个容器. 2.数组的好处:           可以自动为数组中的元素从零开始编号,方便操作这些数据. 3.格式:  (一 ...

随机推荐

  1. HashSet与HashMap的区别

    本文由 ImportNew - 唐小娟 翻译自 Javarevisited.欢迎加入翻译小组.转载请见文末要求. HashMap和HashSet的区别是Java面试中最常被问到的问题.如果没有涉及到C ...

  2. centos 7.0 PHP 5.6.5 安装过程 (php+nginx)

    php网址 http://php.net/downloads.php 首先下载 php-5.6.5.tar.gz [root@localhost src]# wget http://cn2.php.n ...

  3. PuzzleGame部分核心算法

    #include   "mainwindow.h" #include   <QGridLayout> #include   <QPushButton> #i ...

  4. <Web 之困 现代Web应用安全指南>一本好书 69.00?

    NET代码安全 界面漏洞防范与程序优化 一. SQL 注入攻击的源头 1. 过滤或转移危险字符 2.  使用SqlParameter类:.NET 框架有一个叫做SqlParameter 的集合类型,可 ...

  5. winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可

    winServer2003除默认端口外的其他端口只能本地访问,关闭防火墙即可

  6. 如何去各型MCU的官网上下载正确的数据手册

    一.背景 感谢老司机左栋,虽然他一直很排斥这个名号 : ) ,可就技术上来说,还是当之无愧的. 弄了1年多单片机了,数据手册不是老员工或者头头给,就是从开发板资料拿.一直没有意识到,官网的东西才是最可 ...

  7. 如何编写可维护的面向对象JavaScript代码

    能够写出可维护的面向对象JavaScript代 码不仅可以节约金钱,还能让你很受欢迎.不信?有可能你自己或者其他什么人有一天会回来重用你的代码.如果能尽量让这个经历不那么痛苦,就可以节省不少时 间.地 ...

  8. java常量池存放在哪里

    运行以下方法: public class Test { public static void main(String[] args) { String str = "abc"; c ...

  9. php导入导出cvs文件格式

    1.导入 <?php header("Content-type: text/html; charset=gb2312"); $fname = $_FILES['myfile' ...

  10. css固定元素位置(fixed)

    来源:http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html fixed是一种特殊的absolute,同样不占文档流,特殊的地方在于fix ...