Right click > Select "View Page Source" to see source code.
EXAMPLE 1:
'''Example of sorting a dictionary by it's values
Public Function GetMostVisitedAnimals(sortStyle As CustomSortEnum, Optional ByVal num_of_results As Integer = 1) As Dictionary(Of String, Integer)
'''Assume dictionary counts number of times an animal has been seen in wild
Dim _dict As New Dictionary(Of String, Integer)
_dict.Add("Cats", 0)
_dict.Add("Lions", 2)
_dict.Add("Dogs", 14)
_dict.Add("Elephants", 12)
_dict.Add("Giraffes", 10)
_dict.Add("Snakes", 10)
_dict.Add("Tigers", 13)
_dict.Add("Horses", 15)
_dict.Add("Ants", 22)
_dict.Add("Ladybugs", 24)
_dict.Add("Mosquitos", 25)
_dict.Add("Blue Whales", 26)
_dict.Add("Great White Sharks", 27)
_dict.Add("Dolphins", 16)
_dict.Add("Squids", 14)
_dict.Add("Flies", 8)
_dict.Add("Cows", 2)
_dict.Add("Squirrels", 1)
_dict.Add("Hummingbirds", 15)
_dict.Add("Hissing Cockroach", 19)
_dict.Add("Sloths", 41)
_dict.Add("Snails", 99)
_dict.Add("Cheetas", 101)
_dict.Add("Grizzly Bears", 22)
_dict.Add("Moose", 23)
Dim sortedDictionary As Dictionary(Of String, Integer)
'''Clamping numbers only allows numbers within a specificed range.
''' ex: Clamp(val to clamp, min As Integer, max As Integer)
num_of_results = Clamp(num_of_results, 1, _dict.Keys.Count)
If sortStyle = CustomSortEnum.Descending Then
sortedDictionary = _dict.OrderByDescending(Function(p) p.Value).Take(num_of_results).ToDictionary(Function(p) p.Key, Function(p) p.Value)
Else
sortedDictionary = _dict.OrderBy(Function(p) p.Value).Take(num_of_results).ToDictionary(Function(p) p.Key, Function(p) p.Value)
End If
'''Simply for demonstration purposes,
'''this will print the sorted dictionary values in the order desired
For Each kvp in sortedDictionary
Console.WriteLine(kvp.Key & " = " & kvp.Value)
Next
Return sortedDictionary
End Function
'''Example usage:
Call GetSortedFeatures(CustomSortEnum.Ascending, 20)