Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Hashtable

Hashtable

Scheduled Pinned Locked Moved C#
regexquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pranu_13
    wrote on last edited by
    #1

    I added elements to hashtable but they are not in the order in which i added. The code is below: Hashtable table = new Hashtable(4); table[new Regex(@"^(-?)(\d*?)\.?(\d{0,1})?$")] = "$1$2.$3"; table[new Regex(@"^(\d*?)\.?(\d{0,1})?(-?)$")] = "$3$1.$2"; table[new Regex(@"^(-?)(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?$")] = "$1$2$3.$4"; table[new Regex(@"^(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?(-?)$")] = "$4$1$2.$3"; I want these elements in the same order. Some times it takes the same order and some times it does not. I know hashtable does not take elements in same order. But is there a way for this?

    D K 3 Replies Last reply
    0
    • P pranu_13

      I added elements to hashtable but they are not in the order in which i added. The code is below: Hashtable table = new Hashtable(4); table[new Regex(@"^(-?)(\d*?)\.?(\d{0,1})?$")] = "$1$2.$3"; table[new Regex(@"^(\d*?)\.?(\d{0,1})?(-?)$")] = "$3$1.$2"; table[new Regex(@"^(-?)(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?$")] = "$1$2$3.$4"; table[new Regex(@"^(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?(-?)$")] = "$4$1$2.$3"; I want these elements in the same order. Some times it takes the same order and some times it does not. I know hashtable does not take elements in same order. But is there a way for this?

      D Offline
      D Offline
      Dave Herren
      wrote on last edited by
      #2

      Yes, this is an effect of hash tables. "Realize that the order with which the items are inserted and the order of the keys in the Keys collection are not necessarily the same. The ordering of the Keys collection is based on the slot the key's item was stored." ref from: http://msdn2.microsoft.com/en-us/library/aa289149(VS.71).aspx[^] If you don't like this behavior then don't use a hash table.

      topcoderjax - Remember, Google is your friend. Try this Custom Google Code Search

      1 Reply Last reply
      0
      • P pranu_13

        I added elements to hashtable but they are not in the order in which i added. The code is below: Hashtable table = new Hashtable(4); table[new Regex(@"^(-?)(\d*?)\.?(\d{0,1})?$")] = "$1$2.$3"; table[new Regex(@"^(\d*?)\.?(\d{0,1})?(-?)$")] = "$3$1.$2"; table[new Regex(@"^(-?)(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?$")] = "$1$2$3.$4"; table[new Regex(@"^(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?(-?)$")] = "$4$1$2.$3"; I want these elements in the same order. Some times it takes the same order and some times it does not. I know hashtable does not take elements in same order. But is there a way for this?

        K Offline
        K Offline
        Kevin McFarlane
        wrote on last edited by
        #3

        I assume you're confined to .NET 1.1 right? Have a look at this A KeyedList implements an ordered key-value list[^] from Marc Clifton. If .NET 2.0 then use OrderedDictionary

        Kevin

        1 Reply Last reply
        0
        • P pranu_13

          I added elements to hashtable but they are not in the order in which i added. The code is below: Hashtable table = new Hashtable(4); table[new Regex(@"^(-?)(\d*?)\.?(\d{0,1})?$")] = "$1$2.$3"; table[new Regex(@"^(\d*?)\.?(\d{0,1})?(-?)$")] = "$3$1.$2"; table[new Regex(@"^(-?)(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?$")] = "$1$2$3.$4"; table[new Regex(@"^(\d{1,3}?)((?:,\d{3})*)\.?(\d{0,1})?(-?)$")] = "$4$1$2.$3"; I want these elements in the same order. Some times it takes the same order and some times it does not. I know hashtable does not take elements in same order. But is there a way for this?

          K Offline
          K Offline
          Kevin McFarlane
          wrote on last edited by
          #4

          You could also try SortedList. It's not as efficient as Hashtable but may not matter for you if you only have a few entries.

          Kevin

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups