Visual Basic .NET » Visual Basic .NET General Discussion
Hash Table -- Maya --


Hello,

In a hashtable, is there a way to do an "order by" or a similar feature to the value field in the hashtable?

Thanks.

Maya

-- BradleyPeter --


There may be, but I never found one. In one application I was reduced to doing the sorting in a loop (Blerrgh!).

But you can see the problem. The whole point about a HashTable is that it isn't ordered. But having said that, most other languages I've used have provided keys() and values() methods that returned ordered sets IIRC - or maybe you had to do a sort() on them ...

Perhaps I ought to extend HashTable and make a SortedHashTable class.
Hmm...

The easiest method, though, would probably be to put the keys (or the values, I suppose) into an array or ArrayList and then sort them. This only works, though, if you only want the keys or values sorted.
ICollection has a CopyTo(Array) method that you could probably use:

mySortedArray = ((myHash.Keys()).CopyTo(anArray)).Sort();

... or something.

It wouldn't be too difficult from there to sort the ListItems in the HashTable in key or value order in a different array/ArrayList, I wouldn't think.

Just some ideas off the top of my head. They are worth what you paid for them.

:)
PAE
-----Original Message-----
From: DotNetDevelopment
Hash Table
Hello,

In a hashtable, is there a way to do an "order by" or a similar feature to the value field in the hashtable?

Thanks.

Maya


-- Maya --


Thank you peter

[Submit Comment]Home