By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,247 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,247 IT Pros & Developers. It's quick & easy.

Sorting a hash table that was loaded randomly

P: n/a
Do you know of a way to load a hash table with random key/value pairs (e.g.,
2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted"
(key sequence) order (1/"One",2/"Two",3/"Three")? The following just returns
them in the order they were loaded:

Dim hshPrimaryKeyInfo as New Hashtable

<snip code to Add entries to hash table>

For Each hshEntry As DictionaryEntry In hshPrimaryKeyInfo

strKey = hshEntry.Key.ToString

strValue = hshEntry.Value.ToString

Next
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
* "Fred Morrison" <fm*******@erols.com> scripsit:
Do you know of a way to load a hash table with random key/value pairs (e.g.,
2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted"
(key sequence) order (1/"One",2/"Two",3/"Three")? The following just returns
them in the order they were loaded:


Why not maintain a sorted collection (for example, an arraylist) in
addition to the hashtable?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2

P: n/a
I think you need a SortedList
System.Collections.SortedList
"Fred Morrison" <fm*******@erols.com> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl...
Do you know of a way to load a hash table with random key/value pairs (e.g., 2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted" (key sequence) order (1/"One",2/"Two",3/"Three")? The following just returns them in the order they were loaded:

Dim hshPrimaryKeyInfo as New Hashtable

<snip code to Add entries to hash table>

For Each hshEntry As DictionaryEntry In hshPrimaryKeyInfo

strKey = hshEntry.Key.ToString

strValue = hshEntry.Value.ToString

Next

Nov 20 '05 #3

P: n/a
I need key/value pairs. I'll take another look at ArrayList, but it didn't
seem to provide both the random access I need and the ability to traverse
the list in "sorted key" order (vs. "unsorted key" order). I think
Microsoft needs to add a Sort method to HashTable to fix this deficiency.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bv************@ID-208219.news.uni-berlin.de...
* "Fred Morrison" <fm*******@erols.com> scripsit:
Do you know of a way to load a hash table with random key/value pairs (e.g., 2/"Two",1/"One",3/"Three") and then iterate through the entries in "sorted" (key sequence) order (1/"One",2/"Two",3/"Three")? The following just returns them in the order they were loaded:


Why not maintain a sorted collection (for example, an arraylist) in
addition to the hashtable?

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #4

P: n/a
* "Fred Morrison" <fm*******@erols.com> scripsit:
I need key/value pairs. I'll take another look at ArrayList, but it didn't
seem to provide both the random access I need and the ability to traverse
the list in "sorted key" order (vs. "unsorted key" order). I think
Microsoft needs to add a Sort method to HashTable to fix this deficiency.


You will have to maintain both, a hashtable and an arraylist.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
I don't get that one...

like I said in my msg: SortedList ?
not good enough?

<reference guide quote>
A SortedList is a hybrid between a Hashtable and an Array. When an element
is accessed by its key using the Item indexer property, it behaves like a
Hashtable. When an element is accessed by its index using GetByIndex or
SetByIndex, it behaves like an Array.

A SortedList internally maintains two arrays to store the elements of the
list; that is, one array for the keys and another array for the associated
values. Each element is a key-and-value pair that can be accessed as a
DictionaryEntry object.

The capacity of a SortedList is the number of elements that the list can
hold. As elements are added to a SortedList, the capacity is automatically
increased as required through reallocation. The capacity can be decreased by
calling TrimToSize or by setting the Capacity property explicitly.

The elements of a SortedList are sorted by the keys either according to a
specific IComparer implementation specified when the SortedList is created
or according to the IComparable implementation provided by the keys
themselves. In either case, a SortedList does not allow duplicate keys.

The index sequence is based on the sort sequence. When an element is added,
it is inserted into SortedList in the correct sort order, and the indexing
adjusts accordingly. When an element removed, the indexing also adjusts
accordingly. Therefore, the index of a specific key-and-value pair might
change as elements are added or removed from the SortedList.

Operations on a SortedList tend to be slower than operations on a Hashtable
because of the sorting. However, the SortedList offers more flexibility by
allowing access to the values either through the associated keys or through
the indexes.

A key cannot be a null reference (Nothing in Visual Basic), but a value can
be a null reference (Nothing).

Indexes in this collection are zero-based.

[C#] The foreach statement of the C# language requires the type of each
element in the collection. Since each element of the Hashtable is a
key-and-value pair, the element type is not the type of the key or the type
of the value. Instead, the element type is DictionaryEntry. For example:
foreach (DictionaryEntry myEntry in myHashtable) {...}
</reference guide quote>

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bv************@ID-208219.news.uni-berlin.de...
* "Fred Morrison" <fm*******@erols.com> scripsit:
I need key/value pairs. I'll take another look at ArrayList, but it didn't seem to provide both the random access I need and the ability to traverse the list in "sorted key" order (vs. "unsorted key" order). I think
Microsoft needs to add a Sort method to HashTable to fix this
deficiency.
You will have to maintain both, a hashtable and an arraylist.

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>

Nov 20 '05 #6

P: n/a
Cor
Hi Dominique,

That is sometimes a problem in the newsgroups, MVP means for some people the
one who knows the best, (Herfried does not say that).

I think you are absolute rigth and you will see Herfried says that also.

Cor
Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.