A couple of weeks ago, I posted a new learning “Ever depended on the order of elements in a HybridDictionary?”. It was then pointed out to me by Steve Maine that the typical hashtable implementation shouldn’t return a ordered list of any type. After some skillful assistance by Dan Fox, he found that one can get a sorted return if integer keys are used instead of string keys. During my learning lesson, I did use integer keys (which works). But when I typed up an example for the blog using string keys, I assumed that this would work the same way. It does not. Here are the results of the sample below:
String keys
2 = 915793710
3 = 306194301
1 = 821758039
6 = 2073203188
7 = 127676433
4 = 836905414
5 = 218444477
8 = 1218696123
9 = 975467952
10 = 610848541
Integer keys
10 = 610848541
9 = 975467952
8 = 1218696123
7 = 127676433
6 = 2073203188
5 = 218444477
4 = 836905414
3 = 306194301
2 = 915793710
1 = 821758039
Here is the sample:
Sub Main(ByVal args As String())
Dim r As New Random(13123123)
Dim c As New HybridDictionary
Dim i As Integer
If args.Length = 0 Then
Console.WriteLine("Cmd Line Args: 0-string key, 1-integer key")
Else
'Show key type
If args(0) = "0" Then
Console.WriteLine("String keys")
Else
Console.WriteLine("Integer keys")
End If
'Add to dictionary
For i = 1 To 50
If args(0) = "0" Then
c.Add(CStr(i), r.Next)
Else
c.Add(i, r.Next)
End If
Next
End If
'Print out list
Dim e As DictionaryEntry
For Each e In c
Console.WriteLine(e.Key & " = " & e.Value)
Next
End Sub
Updated: formatting