Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HashTable sorting 2

Status
Not open for further replies.

developer155

Programmer
Jan 21, 2004
512
US
I have a hashtable and I add stuff to it in the following order:
5454 -FirstName
6822 - LastName
0258 - Address

etc...
It is stored as a variable in one class. When I access that variable in another class and go through the values, I noticed that values are not in the order they were placed. I really need them to be in the same order. Why does the order change?

thanks
 
You are somewhat breaking 2 design concepts by asking for that...

The first - is that Data is Data. You shouldn't care what order it is stored in until you are ready to display it or if you are running some kind of search/sort on it.

The second - a hash table is a simple key-value pair. If you want a sorted list, I would recommend an ArrayList which provides a .Sort function if you are using the 1.1 framework, otherwise use a List<> if you are using the 2.0 framework.

So what is the purpose of having this data in order? I'm sure we can help you find a better way to work with that data.
 
Like JurkMonkey says. A Hashtable, by design, does not preserve order. Same with Dictionary<T>

If you need to preserve order, use an ArrayList or a List<T>

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top