C# Hashtable 按键排序

发布于 2025-01-05 01:57:02 字数 196 浏览 4 评论 0原文

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

ExchangeA, 200
ExchangeV, 100
ExchangeC, 200

像这样

ExchangeA, 200
ExchangeC, 200
ExchangeV, 100

I have a hashtable with keys in alphabetic and values in numeric.
how to sort the hashtable based on keys?

ExchangeA, 200
ExchangeV, 100
ExchangeC, 200

to be like this

ExchangeA, 200
ExchangeC, 200
ExchangeV, 100

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

伪装你 2025-01-12 01:57:02

您可以使用 SortedDictionary 来进行排序给你的钥匙。在您的情况下, SortedDictionary 可以工作:

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

You can use a SortedDictionary for this which will do the sorting by key for you. In your case a SortedDictionary<string, int> would work:

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);
}

Output:

Key = Exchange A, Value = 200
Key = Exchange C, Value = 200
Key = Exchange V, Value = 100
じ违心 2025-01-12 01:57:02

我发现“排序”哈希表的最简单方法是:

var hash = new Hashtable();
var orderedKeys = hash.Keys.Cast<string>().OrderBy(c => c); // supposing you're using string keys
var allKvp = from x in orderedKeys select new{ key = x, value = hash[x] };

但是,我没有对原始哈希表进行排序,只是以有序的方式读取其值。

正如在其他回复中一样,如果您需要以排序方式存储数据,最好是使用 SortedDictionary

The simplest way I found to "sort" hashtable is:

var hash = new Hashtable();
var orderedKeys = hash.Keys.Cast<string>().OrderBy(c => c); // supposing you're using string keys
var allKvp = from x in orderedKeys select new{ key = x, value = hash[x] };

However, Im not ordering the original hashtable, only reading its values in an ordered way.

As in other replies, if you need to store your data is sorted way, the best is to use SortedDictionary

奶气 2025-01-12 01:57:02

由于哈希表的性质,您无法在适当的键上对它们进行排序:它们根据哈希码将键组织在存储桶中,哈希码是哈希表控制之外的值。但是,您可以按照您喜欢的任何顺序读取键值对。以下是使用 LINQ 实现此操作的方法:

IDictionary<string, int> d = ...; // your hash table
var ordered = d.OrderBy(p => p.Key).ToList();
foreach (var p in ordered) {
    Console.WriteLine("Key: {0} Value: {1}", p.Key, p.Value);
}

Because of the nature of hash tables, you cannot sort them on the key in place: they organize their keys in buckets based on their hash code, a value outside of hash table's control. However, you can read key-value pairs in whatever order that you like. Here is how you can do it using LINQ:

IDictionary<string, int> d = ...; // your hash table
var ordered = d.OrderBy(p => p.Key).ToList();
foreach (var p in ordered) {
    Console.WriteLine("Key: {0} Value: {1}", p.Key, p.Value);
}
落日海湾 2025-01-12 01:57:02

使用列表而不是哈希(或将哈希转换为字典),然后执行以下操作:

var dictionary = new Dictionary<string, int>();
var l = dictionary.Keys.ToList();
l.Sort();
foreach (var key in l)
{
    Console.WriteLine(dictionary[key]);
}

Use a list instead of a hash (or convert your hash to a dictionary), and do this:

var dictionary = new Dictionary<string, int>();
var l = dictionary.Keys.ToList();
l.Sort();
foreach (var key in l)
{
    Console.WriteLine(dictionary[key]);
}
一指流沙 2025-01-12 01:57:02

使用 Linq 很简单(使用 System.Linq):

var sortedList = (from kv in MyDictionary select kv order by kv.Key).ToList<KeyValuePair<string, int>>();

返回一个 KeyValuePair 列表。

Using Linq is easy (using System.Linq):

var sortedList = (from kv in MyDictionary select kv order by kv.Key).ToList<KeyValuePair<string, int>>();

That returns a list of KeyValuePair<string, int>.

心病无药医 2025-01-12 01:57:02

我使用一个列表来存储 Hashtable 的键并对它进行排序,然后使用这个排序列表显示 Hashtable。这是我的代码:

 列表; lst = new List<字符串>();
        foreach(ht.Keys 中的 var key2)
        {
            lst.Add(key2.ToString());
        }
        lst.Sort();
        foreach(lst 中的 var 项目)
        {
            Console.WriteLine(string.Format("{0},{1}", item, ht[item.ToString()]));
        }

I used a list to store keys of Hashtable and sorted it and then dislayed Hashtable using this sorted list. Here is my code:

        List<string> lst = new List<string>();
        foreach (var key2 in ht.Keys)
        {
            lst.Add(key2.ToString());
        }
        lst.Sort();
        foreach (var item in lst)
        {
            Console.WriteLine(string.Format("{0},{1}", item, ht[item.ToString()]));
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文