I have a list of keys and values that I would like sorted by key via a custom Comparer(Of T)
.
I tried using a SortedDictionary
, but kept getting incorrect results because it used the comparer to see if the items were the same. For example calling SortedDictionary.ContainsKey()
would return false, even though it did contain the key.
When I stepped through the code after calling ContainsKey()
, it would go to the comparer.Compare(x, y)
function. It would then only compare against a few of the keys in the dicionary, and somehow skip the matching item (which I ensured did exist). I take it that this is some sort of optimization, where some items are skipped depending on what is returned by the comparer.Compare()
function?
Is it possible to have a dictionary that only uses the comparer for sorting? Or any ideas on some way to overcome this?
Thanks a lot!
EDIT: I am using a Type
object for the dictionary's key
EDIT: The comparer code: (Renderer's for a game engine that get sorted depending on their DrawOrder Property (if they have it))
Public Class RendererComparer
Inherits Comparer(Of Type)
Public Overrides Function Compare(ByVal x As Type, ByVal y As Type) As Integer
If x Is y Then Return 0
If x Is Nothing Then
Return -1
Else
Dim draworderx As Integer = GetDrawOrder(x)
Dim drawordery As Integer = GetDrawOrder(y)
If draworderx < drawordery Then
Return -1
Else
Return 1
End If
End If
End Function
Private Function GetDrawOrder(ByVal t As Type) As Integer
Dim p As PropertyInfo = t.GetProperty("DrawOrder")
If p Is Nothing Then Return 0
Dim o As Object = p.GetValue(Nothing, Nothing)
Return CInt(o)
End Function
End Class