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的使用.还有其他的写法,但发现下边的写 ...
随机推荐
- 安装 cgilib 0.5
下载软件包下载链接:http://pan.baidu.com/s/1gdzOiVT 解包tar zxvf cgilib-0.5.tar.gzcd cgilib-0.5 makecp libcgi.a ...
- ABP官方文档翻译 3.7 领域事件(事件总线)
领域事件(事件总线) 事件总线 注入IEventBus 获取默认实例 定义事件 预定义事件 处理异常 实体更改 触发事件 处理事件 处理基础事件 处理者异常 处理多个事件 注册处理者 自动 手动 取消 ...
- SaltStack 与 Python 程序的结合
SaltStack 库中的 Modules: 在 SaltStack 中,每个子系统插件(plug-in)都是一个 Python Module.因此,SaltStack 库中的 Module 可以看作 ...
- R语言-主成分分析
1.PCA 使用场景:主成分分析是一种数据降维,可以将大量的相关变量转换成一组很少的不相关的变量,这些无关变量称为主成分 步骤: 数据预处理(保证数据中没有缺失值) 选择因子模型(判断是PCA还是EF ...
- 【Tools】ubuntu16.04升级Python2.7到3.5
最近开始学Python,但我发现我ubuntu16.04上默认的Python是2.7,并不是3,x 于是准备Python升级,记录安装过程给初学者参考一下. 1.先取得管理员权限, 个人习惯先取得管理 ...
- 在 Mac 中安装 MySQLdb (Python mysql )
安装环境:OS X操作系统,Python 2.7.3. MySQLdb其实包含在MySQL-python包中,因此无论下载还是在pip中search,都应该是搜寻MySQL-python. 以下将说明 ...
- python学习:使用正则收集ip信息
使用正则表达式收集主机信息 #!/usr/bin/env python from subprocess import Popen, PIPE import re def ge ...
- Redis基础及入门
一. 什么是 Redis Redis 是一个可基于内存,有着完备的持久化机制并以 Key-Value 形式存储的非关系型数据库.也称为数据结构服务器. 二. Redis 的 ...
- 韩信点兵(hanxin)
相传韩信才智过人,从不直接清点自己军队的人数,只要让士兵先后以三人一排.五人一排.七人一排地变换队形,而他每次只掠一眼队伍的排尾就知道总人数了.输入包含多组数据,每组数据包含3个非负整数a,b,c,表 ...
- ubuntu17.10 python3.6 install plugins for AI
install order: tensorflow-gpu scikit-learn numpy scipy matplotlib tkinter tensorflow-gpu : pip insta ...