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的使用.还有其他的写法,但发现下边的写 ...
随机推荐
- windows单节点下安装es集群
linux下的es的tar包,拖到windows下,配置后,启动bin目录下的bat文件,也是可以正常运行的. 从linux下拷的tar包,需要修改虚拟机的内存elasticsearch.in.bat ...
- BZOJ 4517: [Sdoi2016]排列计数 [容斥原理]
4517: [Sdoi2016]排列计数 题意:多组询问,n的全排列中恰好m个不是错排的有多少个 容斥原理强行推♂倒她 $恰好m个不是错排 $ \[ =\ \ge m个不是错排 - \ge m+1个不 ...
- BZOJ 4455: [Zjoi2016]小星星 [容斥原理 树形DP]
4455: [Zjoi2016]小星星 题意:一个图删掉一些边形成一棵树,告诉你图和树的样子,求让图上的点和树上的点对应起来有多少方案 看了很多题解又想了一段时间,感觉题解都没有很深入,现在大致有了自 ...
- CF 2015 ICL, Finals, Div. 1 J. Ceizenpok’s formula [Lucas定理]
http://codeforces.com/gym/100633/problem/J Lucas定理P不是质数裸题 #include <iostream> #include <cst ...
- php与web页面交互
一.web表单 web表单的功能是让浏览者和网站有一个互动的平台.web表单主要用来在网页中发送数据到服务器. 1.1 表单的创建 使用form标记,并在其中插入相关的表单元素,即可创建一个表单. & ...
- 原生js总结(干货)
1.js基本数据类型 number string boolean underfined null 2.查找文档中的特定元素 document.getElementById("id" ...
- 前端开发利器webStorm
这里推荐一个前端开发工具webStorm.用了大概快半年了,发现所有其他工具无出其右的.目前最新版本已经到4.0.2,半年前还是2.X 相比aptana.dreamweaver.sublime和vim ...
- MongoDB 搭建可复制群集
一.概述 MongoDB复制群集支持节点故障自动切换,最小配置应包含3个节点,正常情况下应该至少包含两个数据节点,第三个节点可以是数据节点也可以是仲裁节点.仲裁节点的作用是当出现偶数节点导致无法仲裁的 ...
- Servlet中文乱码问题解决办法
首先对于源jsp网站和servlet里面的字符集要一样,一般支持中文的字符集为UTF-8最好采用这个字符集(除此之外还有gb2312); 对于源jsp文件的代码中需要设置 设置你的page里面的字符集 ...
- MySQL的BlackHole引擎在主从架构中的作用
MySQL在5.x系列提供了Blackhole引擎–“黑洞”. 其作用正如其名字一样:任何写入到此引擎的数据均会被丢弃掉, 不做实际存储:Select语句的内容永远是空. 和Linux中的 /dev/ ...