Hashtable sorting problem.Data appeared in different order
I am attaching a HashTable to the combobox in WPF. The problem is that when the program is run i am seeing the values in a different ord开发者_开发问答er than i originally entered.
Please tell me why is it happening so.
Thanks in advance.
Regards,
John.
A hashtable does not enforce any ordering. Generally, list enforce insertion order. You can try using List<T>
and inserting KeyValuePair<K,V>
if you need the key value pairs. If you really need a dictionary, you can try using the SortedDictionary<K,V>
class.
The order of enumeration on HashTable
, HashSet<T>
, Dictionary<T>
etc. is unspecified. In typical implementations the elements get placed in slots in a way that's convenient for creating an efficient hash table.
It does not make any attempt to preserve the order in which elements were added.
The
HashSet<T>
class provides high-performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.
A hash table orders its elements by the hash of the key, not by the order they are added to the hash table.
Solved the problem using Generic SortedList . Thanks for everyone who helped.
**
The ordering of the Keys collection is based on the slot the key's item was stored. The slot an item is stored depends on the key's hash value and collision resolution strategy.
**
To retrieve an item from the Hashtable, you can index the Hashtable by the key, just like you would index an array by an ordinal value. The following short C# program demonstrates this concept. It adds a number of items to a Hashtable, associating a string key with each item. Then, the particular item can be accessed using its string key.
using System; using System.Collections;
public class HashtableDemo { private static Hashtable employees = new Hashtable();
public static void Main()
{
// Add some values to the Hashtable, indexed by a string key
employees.Add("111-22-3333", "Scott");
employees.Add("222-33-4444", "Sam");
employees.Add("333-44-55555", "Jisun");
// Access a particular key
if (employees.ContainsKey("111-22-3333"))
{
string empName = (string) employees["111-22-3333"];
Console.WriteLine("Employee 111-22-3333's name is: " + empName);
}
else
Console.WriteLine("Employee 111-22-3333 is not in the hash table...");
}
} This code also demonstrates the ContainsKey() method, which returns a Boolean indicating whether or not a specified key was found in the Hashtable. The Hashtable class contains a Keys property that returns a collection of the keys used in the Hashtable. This property can be used to enumerate the items in a Hashtable, as shown below:
// Step through all items in the Hashtable foreach(string key in employees.Keys) Console.WriteLine("Value at employees[\"" + key + "\"] = " + employees[key].ToString()); 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. The slot an item is stored depends on the key's hash value and collision resolution strategy. If you run the above code you can see that the order the items are enumerated doesn't necessarily match with the order with which the items were added to the Hashtable. Running the above code outputs:
Value at employees["333-44-5555"] = Jisun Value at employees["111-22-3333"] = Scott Value at employees["222-33-4444"] = Sam Even though the data was inserted into the Hashtable in the order "Scott," "Sam," "Jisun."
http://msdn.microsoft.com/en-us/library/ms379571(v=vs.80).aspx
精彩评论