C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Then: We invoke the Sort instance method on the keys collection. It requires no argument.
Next: We use the for-each loop construct to loop over the List collection, and do lookups for all values.
For Each, ForVB.NET program that sorts string keys in Dictionary
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
cat -> 1
dog -> 0
eel -> 3
mouse -> 5
programmer -> 2
But: It is ordered first in the alphabetical printout, establishing the program's correctness here.
But: The collections you can obtain from the Dictionary (such as List) can be sorted.
And: This enables a greater degree of opportunity for programmatic conversions. It tends to also make programs more complex.