473,467 Members | 1,596 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Sorting a hash table that was loaded randomly

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
6 7807
* "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
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
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
* "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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Bill Burwell | last post by:
I am converting a VB6 WebClass application to VB.Net. Used the VB upgrade tool to do the conversion - and it left me a lot of little code things to do. Did those code things and got my app to...
7
by: Matthias Käppler | last post by:
Hi, I need to store objects of a FileInfo class containing information about a specific file. I can uniquely identify these files by their serial number. I thought it would be a good idea to use...
8
by: guitarromantic | last post by:
Hey, I have a 'staff' table with a column 'status', corresponding to a 'statuses' table with the appropriate titles (eg 1 | Editor in Chief). I want to display on the page the headings (the...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
6
by: fdmfdmfdm | last post by:
This might not be the best place to post this topic, but I assume most of the experts in C shall know this. This is an interview question. My answer is: hash table gives you O(1) searching but...
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
4
by: arnuld | last post by:
This program follows from the section 6.5 of K&R2 where authors created a doubly-linked list using a binary-tree based approach. The only thing I have rewritten myself is the getword function. I am...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.