c# Hashtable 按键排序

标签 c# sorting hashtable jquery-ui-sortable

我有一个哈希表,其中的键是字母的,值是数字的。 如何根据键对哈希表进行排序?

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/

相关文章:

powershell - 哈希表 - 按输入方式排序

c# - F# 和 C# 的 CLR 相同那么为什么 F# 比 C# 快

c# - 使用 HttpContent.ReadAsAsync<T> 解析带有一个对象或数组的响应

sql - 需要有关 SQL 中复杂排序的帮助

arrays - 如何使用 Data.Vector.Generic.Mutable 进行排序?

algorithm - 考试中的哈希表

Java-如何从 HashMap 创建 Java Hashtable

c# - 常规按钮元素提交表单,这怎么可能?

c# - 使用 msmq 队列对服务进行负载平衡?

java - 如何使用方法按升序和降序输入数组?