Hashtable items order

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nadav

    #1

    Hashtable items order

    Hello.
    I have a Hashtable, to which I insert key and values.
    When I go over the hashtable with "foreach" later on,
    the items are not coming out at the order I inserted them.
    A quickwatch showed that as the items are inserted, they are not arranged in
    the hash at the order they were inserted by.

    Is there a way to control that with a hashtable ? I can't even sort it...
    Thanks


  • Jon Skeet [C# MVP]

    #2
    Re: Hashtable items order

    Nadav <nadav3@gmail.c om> wrote:[color=blue]
    > I have a Hashtable, to which I insert key and values.
    > When I go over the hashtable with "foreach" later on,
    > the items are not coming out at the order I inserted them.[/color]

    No, they wouldn't (necessarily).
    [color=blue]
    > A quickwatch showed that as the items are inserted, they are not arranged in
    > the hash at the order they were inserted by.
    >
    > Is there a way to control that with a hashtable ? I can't even sort it...[/color]

    Hashtables aren't inherently ordered - they're maps. You could use
    SortedList (which is actually a map, despite the name, sorted by key),
    but if you want insertion order, it's probably easiest to keep a
    parallel ArrayList.

    --
    Jon Skeet - <skeet@pobox.co m>
    http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
    If replying to the group, please do not mail me too

    Comment

    • Vadym Stetsyak

      #3
      Re: Hashtable items order

      AFAIK no.

      You can create wrapper that will consist of hashtable for fast search and a
      list for iteration.
      That is when you do myCustHashtable[key] - intrnal hashtable is involved,
      and when you do foreach(Key k in myCustHashtable .Keys) - list is involved.

      With this approach you have to keep internal hastable and list in
      synchronized state.

      --
      Vadym Stetsyak aka Vadmyst
      Blog about software development, algorithms, network protocols, .NET, programming languages, tips &amp; tricks, coding techniques and more.


      "Nadav" <nadav3@gmail.c om> wrote in message
      news:u881C%23sC GHA.1088@tk2msf tngp13.phx.gbl. ..[color=blue]
      > Hello.
      > I have a Hashtable, to which I insert key and values.
      > When I go over the hashtable with "foreach" later on,
      > the items are not coming out at the order I inserted them.
      > A quickwatch showed that as the items are inserted, they are not arranged
      > in
      > the hash at the order they were inserted by.
      >
      > Is there a way to control that with a hashtable ? I can't even sort it...
      > Thanks
      >
      >[/color]


      Comment

      • Michael Nemtsev

        #4
        Re: Hashtable items order

        Hello Nadav,

        Look at System.Collecti ons.Specialized .NameObjectColl ectionBase (ms-help://MS.MSDNQTR.v80. en/MS.MSDN.v80/MS.NETDEVFX.v20 .en/cpref2/html/T_System_Collec tions_Specializ ed_NameObjectCo llectionBase.ht m)

        There is an example how to build smth like Hashlist - to keep keys/values
        and get them by order

        It should helps

        N> I have a Hashtable, to which I insert key and values.
        N> When I go over the hashtable with "foreach" later on,
        N> the items are not coming out at the order I inserted them.
        N> A quickwatch showed that as the items are inserted, they are not
        N> arranged in
        N> the hash at the order they were inserted by.
        N> Is there a way to control that with a hashtable ? I can't even sort
        N> it... Thanks
        N>
        ---
        WBR,
        Michael Nemtsev :: blog: http://spaces.msn.com/members/laflour

        "At times one remains faithful to a cause only because its opponents do not
        cease to be insipid." (c) Friedrich Nietzsche


        Comment

        • Bruce Wood

          #5
          Re: Hashtable items order

          If you insist on using a Hashtable (which you might, because insertion
          speed is a lot faster than for SortedList), the only way you can sort
          is to do a ToArray() and then sort the resulting array on demand.

          However, as Jon pointed out, there is SortedList, which is probably
          what you want.

          Comment

          Working...