Captain Comic
Captain Comic

Reputation: 16196

Using SortedDictionary - getting next value

I am using SortedDictionary to store values sorted by integer.

I need to get next value after specific existing integer. I would prefer working with enumerator but there is no GetEnumerator(Key k) or similar function.

SortedDictionary<int, MyClass> _dict;


void GetNextValue(int start, out MyClass ret)
{
}

Upvotes: 6

Views: 4641

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062745

With reference to the link that Ani added, in this case it would be something like:

ret = source.SkipWhile(pair => pair.Key <= start).First().Value;

or maybe (to allow a Try-style usage)

using(var iter = source.GetEnumerator()) {
    while(iter.MoveNext()) {
        if(iter.Current.Key > start) {
             ret = iter.Current.Value;
             return true;
        }
    }
    ret = null;
    return false;
}

Upvotes: 2

Related Questions