Sorting a hash table that was loaded randomly

  • Thread starter Thread starter Fred Morrison
  • Start date Start date
F

Fred Morrison

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
 
* "Fred Morrison said:
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?
 
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.
 
* "Fred Morrison said:
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.
 
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>
 
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
 
Back
Top