Jon Box's Weblog

 


.NET Articles by the Atomic group

MSDN RDs









Subscribe to "Jon Box's Weblog" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.

 

 

  Sunday, January 25, 2004


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


4:40:51 PM    comment []


Click here to visit the Radio UserLand website. © Copyright 2004 Jon Box.
Last update: 9/1/2004; 12:05:26 AM.

January 2004
Sun Mon Tue Wed Thu Fri Sat
        1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Dec   Feb