Note that there are some explanatory texts on larger screens.

plurals
  1. POSortedDictionary - Comparer only for sorting
    text
    copied!<p>I have a list of keys and values that I would like sorted by key via a custom <code>Comparer(Of T)</code>.</p> <p>I tried using a <code>SortedDictionary</code>, but kept getting incorrect results because it used the comparer to see if the items were the same. For example calling <code>SortedDictionary.ContainsKey()</code> would return false, even though it did contain the key. </p> <p>When I stepped through the code after calling <code>ContainsKey()</code>, it would go to the <code>comparer.Compare(x, y)</code> 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 <code>comparer.Compare()</code> function?</p> <p>Is it possible to have a dictionary that only uses the comparer for sorting? Or any ideas on some way to overcome this?</p> <p>Thanks a lot!</p> <p>EDIT: I am using a <code>Type</code> object for the dictionary's key</p> <p>EDIT: The comparer code: (Renderer's for a game engine that get sorted depending on their DrawOrder Property (if they have it))</p> <pre><code>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 &lt; 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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload