Dictionary排序
有时候由于某些要求会对Dictionary排序,一般有两种方法。
1、使用SortedDictionary。
这种自动会对保存的值进行排序。
- static void Main(string[] args)
- {
- SortedDictionary<int,
object> testDictioary = new SortedDictionary<int,
object>(); - int flag = 0;
- do
- {
- Random random = new Random();
- int temp = random.Next(100);
- if (!testDictioary.ContainsKey(temp))
- {
- testDictioary.Add(temp, null);
- }
- flag = testDictioary.Count;
- } while (flag < 20);
- Console.WriteLine("未排序前:");
- foreach (int key
in testDictioary.Keys) - {
- Console.Write(string.Format(@"{0} ", key));
- }
static void Main(string[] args) { SortedDictionary<int, object> testDictioary = new SortedDictionary<int, object>(); int flag = 0; do { Random random = new Random(); int temp = random.Next(100); if (!testDictioary.ContainsKey(temp)) { testDictioary.Add(temp, null); } flag = testDictioary.Count; } while (flag < 20); Console.WriteLine("未排序前:"); foreach (int key in testDictioary.Keys) { Console.Write(string.Format(@"{0} ", key)); } }
结果:
2、自己写的方法。如下
- public static
void Sort(Dictionary<int,
object> dictionary) - {
- try
- {
- List<int> sortList =
new List<int>(); - Dictionary<int,
object> tempDictionary = new Dictionary<int,
object>(); - foreach (int key
in dictionary.Keys) - {
- sortList.Add(key);
- tempDictionary.Add(key, dictionary[key]);
- }
- int flag = 1;
- int i, j;
- int itemCount = sortList.Count;
- int itemTemp;
- for (i = 1; i < itemCount && flag == 1; i++)
- {
- flag = 0;
- for (j = 0; j < itemCount - i; j++)
- {
- int countfore = sortList[j];
- int countback = sortList[j + 1];
- if (countfore > countback)
- {
- flag = 1;
- itemTemp = sortList[j];
- sortList[j] = sortList[j + 1];
- sortList[j + 1] = itemTemp;
- }
- }
- }
- dictionary.Clear();
- for (int n = 0; n < itemCount; n++)
- {
- foreach (int tempKey
in tempDictionary.Keys) - {
- int value = sortList[n];
- if (tempKey.Equals(value))
- {
- if (!dictionary.ContainsKey(tempKey))
- {
- dictionary.Add(tempKey, tempDictionary[tempKey]);
- }
- }
- }
- }
- }
- catch { }
- }
public static void Sort(Dictionary<int, object> dictionary) { try { List<int> sortList = new List<int>(); Dictionary<int, object> tempDictionary = new Dictionary<int, object>(); foreach (int key in dictionary.Keys) { sortList.Add(key); tempDictionary.Add(key, dictionary[key]); } int flag = 1; int i, j; int itemCount = sortList.Count; int itemTemp; for (i = 1; i < itemCount && flag == 1; i++) { flag = 0; for (j = 0; j < itemCount - i; j++) { int countfore = sortList[j]; int countback = sortList[j + 1]; if (countfore > countback) { flag = 1; itemTemp = sortList[j]; sortList[j] = sortList[j + 1]; sortList[j + 1] = itemTemp; } } } dictionary.Clear(); for (int n = 0; n < itemCount; n++) { foreach (int tempKey in tempDictionary.Keys) { int value = sortList[n]; if (tempKey.Equals(value)) { if (!dictionary.ContainsKey(tempKey)) { dictionary.Add(tempKey, tempDictionary[tempKey]); } } } } } catch { } }
调用结果如下:
- static void Main(string[] args)
- {
- Console.WriteLine("key为数字");
- Dictionary<int,
object> testDictioary = new Dictionary<int,
object>(); - int flag = 0;
- do
- {
- Random random = new Random();
- int temp = random.Next(100);
- if (!testDictioary.ContainsKey(temp))
- {
- testDictioary.Add(temp,
null); - }
- flag = testDictioary.Count;
- } while (flag < 20);
- Console.WriteLine("未排序前:");
- foreach (int key
in testDictioary.Keys) - {
- Console.Write(string.Format(@"{0} ", key));
- }
- Console.WriteLine();
- CustomMethod.Sort(testDictioary);
- Console.WriteLine("排序后:");
- foreach (int key
in testDictioary.Keys) - {
- Console.Write(string.Format(@"{0} ", key));
- }
- Console.ReadLine();
- }
static void Main(string[] args) { Console.WriteLine("key为数字"); Dictionary<int, object> testDictioary = new Dictionary<int, object>(); int flag = 0; do { Random random = new Random(); int temp = random.Next(100); if (!testDictioary.ContainsKey(temp)) { testDictioary.Add(temp, null); } flag = testDictioary.Count; } while (flag < 20); Console.WriteLine("未排序前:"); foreach (int key in testDictioary.Keys) { Console.Write(string.Format(@"{0} ", key)); } Console.WriteLine(); CustomMethod.Sort(testDictioary); Console.WriteLine("排序后:"); foreach (int key in testDictioary.Keys) { Console.Write(string.Format(@"{0} ", key)); } Console.ReadLine(); }
那么这种方法是否是多此一举呢,因为SortedDictionary完全可以满足排序了。但是有时key会是这样的,100+“ssdd”,或100+20,是字符串类型。这样就能用到上面的方法了。比如以key为100+20这种类型为例。改动一下Sort方法。
- public static
void Sort(Dictionary<string,
object> dictionary) - {
- try
- {
- List<int> sortList =
new List<int>(); - Dictionary<string,
object> tempDictionary = new Dictionary<string,
object>(); - foreach (string key
in dictionary.Keys) - {
- int intKey = Convert.ToInt32(key.Substring(0, key.IndexOf("+")));
- sortList.Add(intKey);
- tempDictionary.Add(key, dictionary[key]);
- }
- int flag = 1;
- int i, j;
- int itemCount = sortList.Count;
- int itemTemp;
- for (i = 1; i < itemCount && flag == 1; i++)
- {
- flag = 0;
- for (j = 0; j < itemCount - i; j++)
- {
- int countfore = sortList[j];
- int countback = sortList[j + 1];
- if (countfore > countback)
- {
- flag = 1;
- itemTemp = sortList[j];
- sortList[j] = sortList[j + 1];
- sortList[j + 1] = itemTemp;
- }
- }
- }
- dictionary.Clear();
- for (int n = 0; n < itemCount; n++)
- {
- foreach (string tempKey
in tempDictionary.Keys) - {
- string value = sortList[n].ToString();
- if (tempKey.StartsWith(string.Format(@"{0}+", value)))
- {
- if (!dictionary.ContainsKey(tempKey))
- {
- dictionary.Add(tempKey, tempDictionary[tempKey]);
- }
- }
- }
- }
- }
- catch { }
- }
public static void Sort(Dictionary<string, object> dictionary) { try { List<int> sortList = new List<int>(); Dictionary<string, object> tempDictionary = new Dictionary<string, object>(); foreach (string key in dictionary.Keys) { int intKey = Convert.ToInt32(key.Substring(0, key.IndexOf("+"))); sortList.Add(intKey); tempDictionary.Add(key, dictionary[key]); } int flag = 1; int i, j; int itemCount = sortList.Count; int itemTemp; for (i = 1; i < itemCount && flag == 1; i++) { flag = 0; for (j = 0; j < itemCount - i; j++) { int countfore = sortList[j]; int countback = sortList[j + 1]; if (countfore > countback) { flag = 1; itemTemp = sortList[j]; sortList[j] = sortList[j + 1]; sortList[j + 1] = itemTemp; } } } dictionary.Clear(); for (int n = 0; n < itemCount; n++) { foreach (string tempKey in tempDictionary.Keys) { string value = sortList[n].ToString(); if (tempKey.StartsWith(string.Format(@"{0}+", value))) { if (!dictionary.ContainsKey(tempKey)) { dictionary.Add(tempKey, tempDictionary[tempKey]); } } } } } catch { } }
调用:
- static
void Main(string[] args) - {
- Console.WriteLine("key为字符串");
- Dictionary<string,
object> testDictioary = new Dictionary<string,
object>(); - int flag = 0;
- do
- {
- Random random = new Random();
- int temp = random.Next(100);
- int tempValue = random.Next(100);
- string keyString =
string.Format(@"{0}+{1}", temp, tempValue); - if (!testDictioary.ContainsKey(keyString))
- {
- testDictioary.Add(keyString,
null); - }
- flag = testDictioary.Count;
- } while (flag < 20);
- Console.WriteLine("未排序前:");
- foreach (string key
in testDictioary.Keys) - {
- Console.Write(string.Format(@"{0} ", key));
- }
- Console.WriteLine();
- CustomOtherSort.Sort(testDictioary);
- Console.WriteLine("排序后:");
- foreach (string key
in testDictioary.Keys) - {
- Console.Write(string.Format(@"{0} ", key));
- }
- Console.ReadLine();
- }
static void Main(string[] args) { Console.WriteLine("key为字符串"); Dictionary<string, object> testDictioary = new Dictionary<string, object>(); int flag = 0; do { Random random = new Random(); int temp = random.Next(100); int tempValue = random.Next(100); string keyString = string.Format(@"{0}+{1}", temp, tempValue); if (!testDictioary.ContainsKey(keyString)) { testDictioary.Add(keyString, null); } flag = testDictioary.Count; } while (flag < 20); Console.WriteLine("未排序前:"); foreach (string key in testDictioary.Keys) { Console.Write(string.Format(@"{0} ", key)); } Console.WriteLine(); CustomOtherSort.Sort(testDictioary); Console.WriteLine("排序后:"); foreach (string key in testDictioary.Keys) { Console.Write(string.Format(@"{0} ", key)); } Console.ReadLine(); }
结果:
详细工程:http://download.csdn.net/detail/yysyangyangyangshan/4114133
Dictionary排序的更多相关文章
- C#语法基础用法Dictionary排序
Dictionary排序 1.先看效果图: 2.核心逻辑如下: Dictionary<int, string> list = new Dictionary<int, string&g ...
- C#字典Dictionary排序(顺序、倒序)
这里是针对.NET版本过低的排序方式,没怎么用过,记录一下: 一.创建字典Dictionary 对象 假如 Dictionary 中保存的是一个网站页面流量,key 是网页名称,值value对应的是网 ...
- Linq list 排序,Dictionary 排序
C# 对List成员排序的简单方法 http://blog.csdn.net/wanzhuan2010/article/details/6205884 LINQ之路系列博客导航 http://www. ...
- C#中Dictionary排序方式
转载自:https://www.cnblogs.com/5696-an/p/5625142.html 自定义类: https://files.cnblogs.com/files/xunhanliu/d ...
- C#中Dictionary<TKey,TValue>排序方式
自定义类: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- c# Dictionary的遍历和排序
c# Dictionary的遍历和排序 c#遍历的两种方式 for和foreach for: 需要指定首位数据.末尾数据.数据长度: for遍历语句中可以改变数据的值: 遍历规则可以自定义,灵活性较高 ...
- c# Dictionary的遍历和排序(转)
c#遍历的两种方式 for和foreach for: 需要指定首位数据.末尾数据.数据长度: for遍历语句中可以改变数据的值: 遍历规则可以自定义,灵活性较高 foreach: 需要实现ienume ...
- C#对 Dictionary进行排序 转
C# .net 3.5 以上的版本引入 Linq 后,字典Dictionary排序变得十分简单,用一句类似 sql 数据库查询语句即可搞定:不过,.net 2.0 排序要稍微麻烦一点,为便于使用,将总 ...
- c# 关于字典dictionary 按时间排序
上文中说到sortedlist 排序是键排序,不符合项目要求问题,接着使用字典dictionary 对value 为时间按照升序排序,问题解决.中间涉及到linq的使用.还有其他的写法,但发现下边的写 ...
随机推荐
- Spring-shiro源码陶冶-DefaultFilter
阅读源码有助于陶冶情操,本文旨在简单的分析shiro在Spring中的使用 简单介绍 Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能 Apache Shiro自带的 ...
- ConcurrentHashMap、CopyOnWriteArrayList、LinkedHashMap
HashMap中未进行同步考虑,而Hashtable在每个方法上加上了synchronized,锁住了整个Hash表,一个时刻只能有一个线程操作,其他的线程则只能等待,在并发的环境下,这样的操作导致H ...
- BZOJ 3551: [ONTAK2010]Peaks加强版 [Kruskal重构树 dfs序 主席树]
3551: [ONTAK2010]Peaks加强版 题意:带权图,多组询问与一个点通过边权\(\le lim\)的边连通的点中点权k大值,强制在线 PoPoQQQ大爷题解传送门 说一下感受: 容易发现 ...
- BZOJ 3879: SvT [虚树 后缀树]
传送门 题意: 多次询问,给出一些后缀,求两两之间$LCP$之和 哈哈哈哈哈哈哈竟然$1A$了,刚才还在想如果写不好这道题下节数学就不上了,看来是上天让我上数学课啊 $Suffix\ Virtual\ ...
- react小结
react基础小结 1. 例子 import React from 'react' import { render } from 'react-dom' // 定义组件 class Hello ext ...
- xBIM WeXplorer xViewer的导航,相机、剖切、隐藏 等操作
目录 基础 xBIM WeXplorer 简要介绍 xBIM WeXplorer xViewer 基本应用 xBIM WeXplorer xViewer 浏览器检查 xBIM WeXplorer xV ...
- linux下卸载已安装的软件
1.先查询该软件是否安装,是否存在 rpm -qa | grep -i teamview 2.根据一中的结果(软件包名称),执行如下命令 rpm -e [软件包名]
- JDBC 基础
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...
- [翻译]编写高性能 .NET 代码 第二章:垃圾回收
返回目录 第二章:垃圾回收 垃圾回收是你开发工作中要了解的最重要的事情.它是造成性能问题里最显著的原因,但只要你保持持续的关注(代码审查,监控数据)就可以很快修复这些问题.我这里说的"显著的 ...
- 码农很忙代理IP系统V1.0版本上线
码农很忙代理IP系统V1.0版本上线 经过为期一个月的重写和测试,新版本的码农很忙代理IP系统已于今日正式上线.新版本拥有更精准的匿名类型识别和更高效的验证调度算法. 新版本仍旧采用ASP.NET B ...