我有一个哈希表,其中的键是字母的,值是数字的。 如何根据键对哈希表进行排序?
ExchangeA, 200
ExchangeV, 100
ExchangeC, 200
变成这样
ExchangeA, 200
ExchangeC, 200
ExchangeV, 100
最佳答案
您可以使用 SortedDictionary
为此,它将为您按键排序。在你的情况下 SortedDictionary<string, int>
会工作:
SortedDictionary<string, int> dict = new SortedDictionary<string, int>();
dict.Add("Exchange C", 200);
dict.Add("Exchange A", 200);
dict.Add("Exchange V", 100);
foreach (var kvp in dict)
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key, kvp.Value);
}
输出:
Key = Exchange A, Value = 200
Key = Exchange C, Value = 200
Key = Exchange V, Value = 100
关于c# Hashtable 按键排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280054/