Hi Guys,
I am using hash code to store items in it but when i retrive data using foreach , data not retrived in order so how i can slove this problem ?
All the best,
Printable View
Hi Guys,
I am using hash code to store items in it but when i retrive data using foreach , data not retrived in order so how i can slove this problem ?
All the best,
Sorry, but you need to use a different data structure if you want some type of order. A hash table does not have any order. One option is to store the hash keys in a list that can be ordered. For example.
Generally speaking though, hash tables are used more for a lookup table than iterating through a list of values that must be ordered. Unless you need to lookup the items by a key I suggest just using a list instead of a hash table.Code:HashTable items = new HashTable();
List<String> order = new List<String>();
public AddValue(String key, Object value)
{
order.Add(key);
items[key] = value;
}
...
...
foreach(String key in order)
{
Object value = items[key];
// Do processing code here
}
I would give monalin some rep if I could because he is dead-on. A hash map is inherently unordered and has o(1) lookup complexity. You are using the wrong data structure if you need insertion order to be preserved.
a hashtable / dictionary is just that... it's a HASH table. it stores items by their HASH value (and does so in order of hash value btw) not by insertion order. I mean, yea, the name may indicate that it does something different... :P
All of us at one time have probably asked the same question. I don't think its a stupid question. At least I knew what his question was, unlike half the people who ask for help on the forum lol.
I don't know, this type of stuff was pretty thoroughly covered in data structures classes in college. maybe there are a lot of self educated people out there (who need to then learn about the differences between data structures and why you'd use one over another).
my comments were supposed to be light hearted and in pure jest.
I am self educated. I have a few years of programming classes but i learned more in my first year programming for a company than I would have learned in 4 years of college, all of which I taught myself.
I think it's an easy mistaken assumption to make, especially if you come from a C++ background and are used to std map.
std map does pretty much the same job as a hashtable - at least from an interface point of view.
However as has been pointed out because of the differences in how the data is stored internally hashtables are unordered whereas std map is ordered.
In answer to the original question, try using a SortedList instead.
Darwen.
Not quite true. There's no guarantee that the hashtable will return the values in the dame order as their hash values. It can literally be any order you can imagine. The golden rule about dictionaries and hashtables is that there is no guarantee at all about ordering unless explicitly mentioned in the docs.
I didn't say how the iterator would return them, I just said how they will be stored, and they are stored by their hash value.
Ah, my bad, i must've misunderstood what you meant by
At least no-one else will make that mistake now :)Quote:
...stores items by their HASH value (and does so in order of hash value btw) ...
Thing is, i've debugged issues before because people assumed that Dictionary<k,V> return values in insertion order when using foreach (). So if anyone writes anything that even has a vague implication that stuff is in a specific order, i jump on it ;)