C# 是否可以对 HashTable 进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/675759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 12:59:32  来源:igfitidea点击:

Is it possible to sort a HashTable?

c#sortinghashtable

提问by user81740

I have a property that returns a HashTable. I would like to sort it without refactoring my property. Please note: I do not want to return another type. Code:

我有一个返回HashTable. 我想在不重构我的属性的情况下对其进行排序。请注意:我不想返回其他类型。代码:

    /// <summary>
    /// All content containers.
    /// </summary>
    public Hashtable Containers
    {
        get
        {
            Hashtable tbl = new Hashtable();
            foreach (Control ctrl in Form.Controls)
            {
                if (ctrl is PlaceHolder)
                {
                    tbl.Add(ctrl.ID, ctrl);
                }
                // Also check for user controls with content placeholders.
                else if (ctrl is UserControl)
                {
                    foreach (Control ctrl2 in ctrl.Controls)
                    {
                        if (ctrl2 is PlaceHolder)
                        {
                            tbl.Add(ctrl2.ID, ctrl2);
                        }
                    }
                }
            }

            return tbl;
        }
    }

采纳答案by joel.neely

Another option is to construct the hash table as you're already doing, and then simply construct a sorted set from the keys. You can iterate through that sorted key set, fetching the corresponding value from the hash table as needed.

另一种选择是像您已经在做的那样构建哈希表,然后简单地从键构建一个排序集。您可以遍历该排序的键集,根据需要从哈希表中获取相应的值。

回答by lubos hasko

Sorry, but you can't sort hashtable. You will have to refactor your code to use some sortable collections.

抱歉,您无法对哈希表进行排序。您将不得不重构您的代码以使用一些可排序的集合。

回答by Daniel Brückner

I am quite sure that hash tables cannot be sorted ... ;)

我很确定无法对哈希表进行排序...;)

Wikipedia Hash Table

维基百科哈希表

回答by Joel Coehoorn

lubos is right: you can'tsort a HashTable. If you could, it wouldn't be a HashTable. You can enumerate the HashTable, and then sort the enumeration. But that would be very slow. Much better to use a SortedDictionaryinstead.

lubos 是对的:你不能对 HashTable 进行排序。如果可以,它就不会是 HashTable。您可以枚举 HashTable,然后对枚举进行排序。但这会很慢。最好使用 aSortedDictionary代替。

回答by Brian R. Bondy

There is no point in sorting a hash table because you already have almost constant lookup time. Or at worst O(B) where B is the bucket size.

对哈希表进行排序是没有意义的,因为您已经拥有几乎恒定的查找时间。或者最坏的 O(B) 其中 B 是桶的大小。

回答by John Feminella

Hashtables work by mapping keys to values. Implicit in this mapping is the concept that the keys aren't sorted or stored in any particular order.

哈希表通过将键映射到值来工作。此映射中隐含的概念是键不按任何特定顺序排序或存储。

However, you could take a look at SortedDictionary<K,V>.

不过,你可以看看SortedDictionary<K,V>

回答by Peter

You will need to return something other than a hash table. I won't reiterate what you claim to understand already, but you need to rethink whatever part of your design requires you to return sorted objects in a hash table.

您将需要返回哈希表以外的其他内容。我不会重申您声称已经了解的内容,但是您需要重新考虑设计的任何部分要求您在哈希表中返回已排序的对象。

回答by Arafangion

Of course hash tables can be sorted, but you need to first define what it means to sort a hash table. (Therein lies the issue)

哈希表当然是可以排序的,但是你需要先定义一下对哈希表进行排序是什么意思。(这就是问题所在)

Once you have done that, however, you've invariably removed all the advantages that a hashtable can give you, and you might as well use a sorted array (with binary searching), or use a red-black tree instead.

然而,一旦你这样做了,你就不可避免地消除了哈希表可以给你带来的所有优势,你不妨使用排序数组(使用二分搜索),或者改用红黑树。

回答by Zan Lynx

Not exactly a C# answer but I am sure you can make something of it.

不完全是 C# 答案,但我相信您可以有所作为。

In Perl it is common to "sort" a hash table for use in output to the display.

在 Perl 中,通常对用于显示的输出的哈希表进行“排序”。

For example:

例如:

print "Items: ";
foreach (sort keys %items) {
    print $_, '=', $items{$_}, ' ';
}

The trick here is that Perl doesn't sort the hash, it is sorting a copied list of hash keys. It should be easy enough in C# to extract the hash keys into a list and then sort that list.

这里的技巧是 Perl 不对散列进行排序,而是对复制的散列键列表进行排序。在 C# 中应该很容易将哈希键提取到列表中,然后对该列表进行排序。

回答by Noah

I am a new programmer so take everything I say with a grain of salt. But here is what I did when I ran into a similar situation. I created a class that had two variables and then created a Listobject off those variables and then I used linq to sort those variables.

我是一名新程序员,所以对我所说的一切持保留态度。但是当我遇到类似的情况时,这就是我所做的。我创建了一个包含两个变量的类,然后根据List这些变量创建了一个对象,然后我使用 linq 对这些变量进行排序。