user81740
user81740

Reputation: 233

Is it possible to sort a HashTable?

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:

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

Upvotes: 15

Views: 40994

Answers (11)

Igor Krupitsky
Igor Krupitsky

Reputation: 885

You can also use DataView to sort the Hashtable. Here is an article that I wrote 5 years ago: http://www.codeproject.com/Articles/37039/Sorting-Hashtable

Upvotes: 0

Noah
Noah

Reputation: 1

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 List object off those variables and then I used linq to sort those variables.

Upvotes: 0

Zan Lynx
Zan Lynx

Reputation: 54325

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

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

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.

Upvotes: 1

Arafangion
Arafangion

Reputation: 11910

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.

Upvotes: 0

joel.neely
joel.neely

Reputation: 30943

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.

Upvotes: 9

Joel Coehoorn
Joel Coehoorn

Reputation: 415860

lubos is right: you can't sort 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 SortedDictionary instead.

Upvotes: 7

Peter
Peter

Reputation: 7324

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.

Upvotes: 1

John Feminella
John Feminella

Reputation: 311536

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>.

Upvotes: 18

Brian R. Bondy
Brian R. Bondy

Reputation: 347266

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.

Upvotes: 0

Daniel Br&#252;ckner
Daniel Br&#252;ckner

Reputation: 59655

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

Wikipedia Hash Table

Upvotes: 1

lubos hasko
lubos hasko

Reputation: 25052

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

Upvotes: 3

Related Questions