Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / June 2007

Tip: Looking for answers? Try searching our database.

RemoveAll for a SortedList

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michael Nesslinger - 14 Jun 2007 18:04 GMT
Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<T> match)" for
a SortedList like it is possible for a List.

My first question is:
Why is the  Method not available for the SortedList.
And my second one:
How can it be done the best (fastest) way for a SortedList

Thanks for any ideas

Michael Neßlinger
Nicholas Paldino [.NET/C# MVP] - 14 Jun 2007 18:26 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:

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.