How, in a .NET, do you search a sorted collection for a key, and get the index, or if it doesn't exist, get the index of the next highest item?
For example there is a list that contains elements {1,5,8,10}. I search for 7. It doesn't exist, but the next highest key that does exist is 8 with an index of 2.
Example:
SortedList<int, int> list = new SortedList<int, int>();
list.Add(1, 1);
list.Add(5, 1);
list.Add(8, 1);
list.Add(10, 1);
int index = list.IndexOfKeyOrNext(7); // theoretical function. returns 2
I can do this by adding a temporary item at 7, calling list.IndexOfKey(7), then removing my temporary item. But This is slow. Is there a better way?
Edit: My list is sorted.