CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2007
    Posts
    122

    hash code not oragnaize data in order >

    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,

  2. #2
    Join Date
    Jul 2006
    Posts
    297

    Re: hash code not oragnaize data in order >

    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.

    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 
    }
    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.
    Last edited by monalin; August 13th, 2009 at 01:17 PM.

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: hash code not oragnaize data in order >

    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.

  4. #4
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: hash code not oragnaize data in order >

    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

  5. #5
    Join Date
    Jul 2006
    Posts
    297

    Re: hash code not oragnaize data in order >

    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.

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: hash code not oragnaize data in order >

    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.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: hash code not oragnaize data in order >

    Quote Originally Posted by MadHatter View Post
    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

  8. #8
    Join Date
    Jul 2006
    Posts
    297

    Re: hash code not oragnaize data in order >

    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.

  9. #9
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: hash code not oragnaize data in order >

    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.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

  10. #10
    Join Date
    May 2007
    Posts
    1,546

    Re: hash code not oragnaize data in order >

    Quote Originally Posted by MadHatter View Post
    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
    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.
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

  11. #11
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: hash code not oragnaize data in order >

    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.

  12. #12
    Join Date
    May 2007
    Posts
    1,546

    Re: hash code not oragnaize data in order >

    Ah, my bad, i must've misunderstood what you meant by

    ...stores items by their HASH value (and does so in order of hash value btw) ...
    At least no-one else will make that mistake now

    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
    www.monotorrent.com For all your .NET bittorrent needs

    NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured