VB.NET Sort Dictionary

by Sam Allen - Updated January 27, 2010

You want to sort a Dictionary of string type keys in the VB.NET programming language, such that you can loop over the string keys in alphabetical order, regardless of the order in which they were added. While the Dictionary data itself cannot be sorted directly, you can obtain the Keys and sort them, and then loop over that. Here, we describe this approach this approach to sorting Dictionary keys in the VB.NET language.

Sorting Dictionary keys

Initially here, we look at the VB.NET programming language source text that adds five key and value pairs to a Dictionary(Of String, Integer). Then, we acquire the list of strings, using the ToList extension method on the Keys property. Then, we invoke the Sort instance method on the keys collection. Next, we use the for each loop construct to loop over the List collection, and do lookups for all values.

--- Program that sorts string keys in Dictionary (VB.NET) ---

Module Module1

    Sub Main()
        ' Create Dictionary with string keys.
        Dim dict As New Dictionary(Of String, Integer)
        dict.Add("dog", 0)
        dict.Add("cat", 1)
        dict.Add("mouse", 5)
        dict.Add("eel", 3)
        dict.Add("programmer", 2)

        ' Get list of keys.
        Dim keys As List(Of String) = dict.Keys.ToList

        ' Sort the keys.
        keys.Sort()

        ' Loop over the sorted keys.
        Dim str As String
        For Each str In keys
            Console.WriteLine("{0} -> {1}", str, dict.Item(str))
        Next
    End Sub

End Module

--- Output of the program ---

cat -> 1
dog -> 0
eel -> 3
mouse -> 5
programmer -> 2

Sort method used. In the .NET Framework, the Sort method on the List type is implemented with a Quick Sort algorithm, making it very efficient on strings. Please note how the string "cat" was added second in the source code, but is ordered first in the alphabetical printout, establishing the program's correctness here.

Written by request. This article was requested by a reader of Dot Net Perls some weeks ago, and it seemed like a good parallel to the C# version that was already available. If you have an article idea, Sam can sometimes write it up.

Summary

In this example, we saw how you can sort the keys in a Dictionary collection using the Keys property to acquire the KeyCollection, and then the ToList and Sort methods to manipulate the elements. While the Dictionary internals themselves cannot be sorted or reordered, the collections you can obtain from the Dictionary can be sorted, and this enables a greater degree of opportunity for programmatic conversions.

(See Dictionary Tips and Examples.)

(Do not copy this page.)

Dot Net Perls | Search
VB.NET | DataGridView Usage | Dictionary Tips and Examples | Split String Examples | Using StreamReader Examples | Using StreamWriter
C# | Obsolete Attribute | True and False | Gradient Tips | Catch Examples
© 2010 Sam Allen. All rights reserved.
Dot Net Perls | Sam Allen