Actually, you can use SortedDictionary
for this. But what you need (assuming the distance is int
) is a SortedDictionary<int, List<Item>>
.
When you want to add a distance that's not in the dictionary yet, you insert a new List
containing a single item. When you want to add a distance that is already in the dictionary, find the right list and add the item to it.
To remove the smallest item, find the List
with the smallest key and remove an item from it. If the List
becomes empty, remove it from the dictionary.
Since removing from the start of a List
is inefficient, you will either need to remove from the end of the List
(resulting in LIFO order for items with the same key), or use a Queue
instead of List
.
This is probably not the most efficient solution, but insertion, finding smallest item and removing smallest item are all O(log n).