1

See the example here, i stored values in this order! But the output i'm getting is different! Why? In what order the Hashtable Storing the Values?

      {               
        Hashtable ht = new Hashtable();
        ht.Add("001", "Zara Ali");
        ht.Add("002", "Abida Rehman");
        ht.Add("003", "Joe Holzner");
        ht.Add("004", "Mausam Benazir Nur");
        ht.Add("005", "M. Amlan");
        ht.Add("006", "M. Arif");
        ht.Add("007", "Ritesh Saikia");

        ICollection key = ht.Keys;

        foreach (string k in key)
         {
            Console.WriteLine(k + ": " + ht[k]);
         }

       }

Ouput

006: M. Arif
007: Ritesh Saikia
003: Joe Holzner
002: Abida Rehman
004: Mausam Benazir Nur
001: Zara Ali
005: M. Amlan
Balaji
  • 1,375
  • 1
  • 16
  • 30

3 Answers3

4

A Hashtable does not guarantee any defined order to the elements within. The implementation of the hashtable splits the values into different buckets based on their Hashcode and its internal implementation, meaning the same values could have different order on different machines, different runs or different versions of the framework. This is because Hashtables are optimized for by-key-retrieval, rather than by-order retrieval.

If you want a collection that can be accessed both by key and in-order, use one of the specialized collections. Judging by your use of Hashtable rather than Dictionary<K,V>, it's possible you're using .NET 1.1, in which case you can use SortedList which will maintain order internally. Newer versions of .NET have SortedList<K,V> and OrderedDictionary<K,V> which differ a bit in their performance characteristics:

Community
  • 1
  • 1
Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
3

You can use SortedDictionary<K, T> instead of obsolete HashTable:

SortedDictionary<String, String> ht = new SortedDictionary<String, String>() {
  {"001", "Zara Ali"},
  {"002", "Abida Rehman"},
  {"003", "Joe Holzner"},
  {"004", "Mausam Benazir Nur"},
  {"005", "M. Amlan"},
  {"006", "M. Arif"},
  {"007", "Ritesh Saikia"}
};

foreach(var pair in ht)
  Console.WriteLine(pair.Key + " " + pair.Value);

since Dictionary<K, T>, Set<T>, HashTable (note, that HashTable as well as ICollection are obsolete) don't preserve order you have to use SortedDictionary<K, T> and SortedSet<T>.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

Hashtable does not guarantee any order.You can use Dictionary<string, string> you can sorted by Value like you want. You can also use SortedDictionary<string, string> this is sorted by key by default.

        Dictionary<string, string> ht = new Dictionary<string, string>();
        ht.Add("001", "Zara Ali");
        ht.Add("002", "Abida Rehman");
        ht.Add("003", "Joe Holzner");
        ht.Add("004", "Mausam Benazir Nur");
        ht.Add("005", "M. Amlan");
        ht.Add("006", "M. Arif");
        ht.Add("007", "Ritesh Saikia");

        var order = ht.OrderBy(x => x.Value);//ht.OrderBy(x => x.Key);

        foreach (var k in order)
        {
            Console.WriteLine(k.Key + ": " + k.Value);
        }
mybirthname
  • 17,949
  • 3
  • 31
  • 55