I have a situation where I have a SortedList, but with very sparse entries. I would like to be able to find values for a range of keys, however I believe my solution is too 'brute force' like and is very slow.
SortedList<ushort, string> myList = new SortedList<ushort, string>();
myList.Add(3, "three");
myList.Add(2, "two");
myList.Add(4, "four");
myList.Add(11, "eleven");
myList.Add(23, "twenty three");
myList.Add(16, "sixteen");
// Get values where Key is > 12 and < 19
for (ushort i = 13; i < 19; i++)
{
if(!myList.ContainsKey(i))
continue;
// Got a key!
}
In the case of my example, brute force might be okay, but if the keys are much larger, with thousands of empty keys between, this becomes a problem, is there a better way to do this?
Thanks.