Sorting VB.Net HashTable sorting by Key *Resolved*

September 6, 2012

So there’s two options you have.

  1. Use SortedList() instead of HashTable()
  2. You can come up with your own Sorted method, see below

Method 1


Imports System.Collections
...
Dim ht As New SortedList()
ht.Add("A4", "4")
ht.Add("A1", "1")
ht.Add("A3", "3")
ht.Add("A2", "2")
For Each k As DictionaryEntry In ht
Console.WriteLine("{0} is {1}", k.Key, k.Value)
Next

Method 2


Imports System.Collections
...
Dim ht As New Hashtable()
ht.Add("A4", "4")
ht.Add("A1", "1")
ht.Add("A3", "3")
ht.Add("A2", "2")
Dim keys As ICollection = ht.Keys
Dim keysA(ht.Count - 1) As String
keys.CopyTo(keysA, 0)
Array.Sort(keysA)
For Each key As String in keysA
Console.WriteLine("{0} is {1}", key, ht(key))
Next

+1 this post if it helped you!

Leave a Reply