C# dictionary排序问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# dictionary排序问题相关的知识,希望对你有一定的参考价值。

我定义了一个
static Dictionary<string, pProcess> nameProcess = new Dictionary<string, pProcess>();
其中pProcess是一个类。这个类里有几个属性。我想按照某个属性给这个Dictionary的值排序,请问有什么简便的方法没

每次取出来的时候order by一下就好了,如果你需要按顺序存储可以用SortedDictionary追问

SortedDictionary是时时都会按顺序进行维护吗?另外他能不能按我的要求,就是按其值的某一属性的大小进行排序。新手不太懂...麻烦了

追答

是,不过是key值,如果你需要时时维护你自己的某一个顺序,你还不如自己写一个类呢,很简单

参考技术A 只能自己写比较器,再调用sort方法

c# Dictionary 字典的排序问题,请大家指点

看图吧

Dictionary 一般不好排序,你 可以使用 SortDictionary 类排序···~~~~~~~~~

参考MSDN:
SortedDictionary 泛型类 表示按键排序的键/值对的集合。
类型参数
TKey 字典中的键的类型。
TValue 字典中的值的类型。

SortedDictionary 泛型类是检索运算复杂度为 O(log n) 的二叉搜索树,其中 n 是字典中的元素数。就这一点而言,它与 SortedList 泛型类相似。这两个类具有相似的对象模型,并且都具有 O(log n) 的检索运算复杂度。这两个类的区别在于内存的使用以及插入和移除元素的速度:

SortedList 使用的内存比 SortedDictionary 少。

foreach (KeyValuePair<int, string> kvp in myDictionary) ...
foreach 语句是对枚举数的包装,只允许该枚举数读取集合,而不允许写入集合。

用法例子:
using System;
using System.Collections.Generic;

public class Example

public static void Main()

// Create a new dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
new Dictionary<string, string>();

// Add some elements to the dictionary. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");

// The Add method throws an exception if the new key is
// already in the dictionary.
try

openWith.Add("txt", "winword.exe");

catch (ArgumentException)

Console.WriteLine("An element with Key = \"txt\" already exists.");


// The Item property is another name for the indexer, so you
// can omit its name when accessing elements.
Console.WriteLine("For key = \"rtf\", value = 0.",
openWith["rtf"]);

// The indexer can be used to change the value associated
// with a key.
openWith["rtf"] = "winword.exe";
Console.WriteLine("For key = \"rtf\", value = 0.",
openWith["rtf"]);

// If a key does not exist, setting the indexer for that key
// adds a new key/value pair.
openWith["doc"] = "winword.exe";

// The indexer throws an exception if the requested key is
// not in the dictionary.
try

Console.WriteLine("For key = \"tif\", value = 0.",
openWith["tif"]);

catch (KeyNotFoundException)

Console.WriteLine("Key = \"tif\" is not found.");


// When a program often has to try keys that turn out not to
// be in the dictionary, TryGetValue can be a more efficient
// way to retrieve values.
string value = "";
if (openWith.TryGetValue("tif", out value))

Console.WriteLine("For key = \"tif\", value = 0.", value);

else

Console.WriteLine("Key = \"tif\" is not found.");


// ContainsKey can be used to test keys before inserting
// them.
if (!openWith.ContainsKey("ht"))

openWith.Add("ht", "hypertrm.exe");
Console.WriteLine("Value added for key = \"ht\": 0",
openWith["ht"]);


// When you use foreach to enumerate dictionary elements,
// the elements are retrieved as KeyValuePair objects.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )

Console.WriteLine("Key = 0, Value = 1",
kvp.Key, kvp.Value);


// To get the values alone, use the Values property.
Dictionary<string, string>.ValueCollection valueColl =
openWith.Values;

// The elements of the ValueCollection are strongly typed
// with the type that was specified for dictionary values.
Console.WriteLine();
foreach( string s in valueColl )

Console.WriteLine("Value = 0", s);


// To get the keys alone, use the Keys property.
Dictionary<string, string>.KeyCollection keyColl =
openWith.Keys;

// The elements of the KeyCollection are strongly typed
// with the type that was specified for dictionary keys.
Console.WriteLine();
foreach( string s in keyColl )

Console.WriteLine("Key = 0", s);


// Use the Remove method to remove a key/value pair.
Console.WriteLine("\nRemove(\"doc\")");
openWith.Remove("doc");

if (!openWith.ContainsKey("doc"))

Console.WriteLine("Key \"doc\" is not found.");




/* This code example produces the following output:

An element with Key = "txt" already exists.
For key = "rtf", value = wordpad.exe.
For key = "rtf", value = winword.exe.
Key = "tif" is not found.
Key = "tif" is not found.
Value added for key = "ht": hypertrm.exe

Key = txt, Value = notepad.exe
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = winword.exe
Key = doc, Value = winword.exe
Key = ht, Value = hypertrm.exe

Value = notepad.exe
Value = paint.exe
Value = paint.exe
Value = winword.exe
Value = winword.exe
Value = hypertrm.exe

Key = txt
Key = bmp
Key = dib
Key = rtf
Key = doc
Key = ht

Remove("doc")
Key "doc" is not found.
*/
参考技术A X 表示以十六进制形式输出
02 表示不足两位,前面补0输出;出过两位,不影响
举例:
printf("%02X", 0x123); //打印出:123
printf("%02X", 0x1); //打印出:01
参考技术B 字典排本身没有提供排序方法,可以通过linq查询实现,大概代码如下,你试试看哦:
public Dictionary<string, string> dictList ;
//这里你自己加入一些字典元素进去
dictList = (from entry in dictList
orderby entry.Value.DeviceIndex ascending
select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
我就是这样排序的,希望对你有用
参考技术C dic = (from entry in dic orderby entry.Key.Length descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

或者
dic = dic.OrderByDescending(r => r.Key.Length).ToDictionary(r => r.Key, r => r.Value);
【请叫我雷锋】

以上是关于C# dictionary排序问题的主要内容,如果未能解决你的问题,请参考以下文章

c# 字典 Dictionary排序问题

c# Dictionary 字典的排序问题,请大家指点

C#字典Dictionary排序(顺序倒序)

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

c#字典dictionary绑定datagridview如何排序

C#创建安全的字典(Dictionary)存储结构