Hashtable
-
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?
-
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?
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
-
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?
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
-
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?
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