Maury Markowitz Maury Markowitz - 7 months ago 25
Vb.net Question

First item in a SortedDictionary?

There are many, many threads on ways to get the "first" item from a

Dictionary
, and various answers as to why such a thing is not really a good idea because there's no internal ordering. But mine's a
SortedDictionary
, so those arguments don't apply. Yet I cannot find a way to get the Nth item from a
SortedDictionary
any easier than a
Dictionary
.

Here is my SD:

FRs As SortedDictionary(Of DateTime, ScheduleItem)


I see a few hints that I should be able to do:

If FRs.Count = 1 Then
FirstFR = FRs.Keys(0)


But that is not valid in my code - it says it has no defaults and cannot be indexed.
.First
and other options appear to all be in LINQ, which I cannot target. So is there any simple way to access it in this fashion?

Note: any solution offered must not use LINQ, which does not exist on many non-Wintel platforms.

Answer

The problem is that a SortedDictionary is indeed ordered by the key. But that does not mean that you can access it via index. So if you can't use LINQ:

Dim firstFR As KeyValuePair(Of DateTime, ScheduleItem)
For Each kv In FRs
    firstFR = kv
    Exit For
Next

Otherwise you could simply use First/ FirstOrDefault.

Sidenote: since a KeyValuePair(Of Tkey, TValue) is a structure, therefore a value type, it is never null/Nothing. You could check for an empty dictionary in this ugly way:

If FirstFR.Equals(New KeyValuePair(Of DateTime, ScheduleItem))
    Console.WriteLine("Empty dictionary")
End If 

So it's much more readable to use If FRs.Count = 0 Then ....