i was searching for a simple solution that i can use in sorting a dictionary by value, and i was not content with what i found, so i made up these two functions for sorting a dictionary ascending and descending. I do not assume that they are perfect, so improvements are welcomed. They are written in VB.Net and use an intermediary dictionary.
Public Function SortDictionaryAsc(ByVal dict As Dictionary(Of Long, Decimal)) As Dictionary(Of Long, Decimal)
Dim final As New Dictionary(Of Long, Decimal)
Dim min As Decimal = dict.Values.ToList.Max
Dim key As Long
Do
min = dict.Values.Max + 1
For Each x In dict.Keys
If dict(x) < min Then
min = dict(x)
key = x
End If
Next
final.Add(key, min)
dict.Remove(key)
Loop While dict.Keys.Count > 0
Return final
End Function
Public Function SortDictionaryDesc(ByVal dict As Dictionary(Of Long, Decimal)) As Dictionary(Of Long, Decimal)
Dim final As New Dictionary(Of Long, Decimal)
Dim max As Decimal = 0
Dim prev As Decimal = 0
Dim key As Long
Do While dict.Keys.Count - 1 > 0
For Each x In dict.Keys
If dict(x) > max Then
max = dict(x)
key = x
End If
Next
final.Add(key, max)
dict.Remove(key)
max = 0
Loop
Return final
End Function