Michael,
The only way you are going to be able to do this is by looping through
each item and then determining if it should be removed:
public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
TValue> sortedList,
Predicate<KeyValuePair<TKey, TValue>> match)
{
// The number of items removed.
int itemsRemoved = 0;
// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<TKey, TValue> pair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remove(pair.Key);
// Increment the number of items removed.
itemsRemoved++;
}
}
// Return the items removed.
return itemsRemoved;
}
Hope this helps.

Signature
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
> Hello,
>
[quoted text clipped - 9 lines]
>
> Michael Neßlinger
Michael Nesslinger - 14 Jun 2007 18:31 GMT
Nicholas Paldino [.NET/C# MVP] schrieb:
> Michael,
>
[quoted text clipped - 27 lines]
>
> Hope this helps.
Thanks,
that is what i was suspecting. I just hoped for some nice trick to avoid
this.
Michael Neßlinger
Ben Voigt [C++ MVP] - 15 Jun 2007 02:21 GMT
> Michael,
>
> The only way you are going to be able to do this is by looping through
> each item and then determining if it should be removed:
But loop from the end, to avoid repeated defragmentation.
> public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
> TValue> sortedList,
[quoted text clipped - 36 lines]
>>
>> Michael Neßlinger
Ben Voigt [C++ MVP] - 15 Jun 2007 02:22 GMT
> Michael,
>
> The only way you are going to be able to do this is by looping through
> each item and then determining if it should be removed:
Stupid me... any sort of repeated removal involves suicidal defragmentation.
Copy items selectively into a new list instead.
> public static int RemoveAllFromSortedList<TKey, TValue>(SortedList<TKey,
> TValue> sortedList,
[quoted text clipped - 36 lines]
>>
>> Michael Neßlinger