473,405 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

RemoveAll for a SortedList

Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" 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
Jun 14 '07 #1
4 4299
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,
TValuesortedList,
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, TValuepair 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Nesslinger" <Mi****************@gmx.dewrote in message
news:46********@news.kullen.rwth-aachen.de...
Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" 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

Jun 14 '07 #2
Nicholas Paldino [.NET/C# MVP] schrieb:
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,
TValuesortedList,
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, TValuepair 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.

Thanks,
that is what i was suspecting. I just hoped for some nice trick to avoid
this.

Michael Neßlinger
Jun 14 '07 #3

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2******************@TK2MSFTNGP06.phx.gbl...
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,
TValuesortedList,
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, TValuepair 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Nesslinger" <Mi****************@gmx.dewrote in message
news:46********@news.kullen.rwth-aachen.de...
>Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" 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


Jun 15 '07 #4

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2******************@TK2MSFTNGP06.phx.gbl...
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,
TValuesortedList,
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, TValuepair 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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Michael Nesslinger" <Mi****************@gmx.dewrote in message
news:46********@news.kullen.rwth-aachen.de...
>Hello,

i am looking for an easy way to do a "RemoveAll(Predicate<Tmatch)" 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


Jun 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gerrod | last post by:
Hi - Does anyone know a way to created a SortedList (in the System.Collections namespace) that will sort on VALUES instead of KEYS... ? The scenario is this - I have a SortedList containing...
2
by: Pekka | last post by:
Could somebody say why the piece of code below does not work? My purpose is to renumber keys in a SortedList (after removal of an item) so that the keys would always contain an unbroken sequence of...
1
by: Guadala Harry | last post by:
I store a few things in my app's Application state. Sometimes I need to change them, so I have been calling Application.RemoveAll() using the following line, and then re-inserting my variables with...
2
by: KrippZ | last post by:
Hello I'm having some problems with a a sortedList in C#. My set is as follows: I have a sortedList as a class variable (DataList) I inisiate it in my constructor with DataList = new SortedList()....
2
by: Prez | last post by:
I started writing .net code yesterday and I am grasping it well enough. I have a few questions about SortedLists. I am using managed C++ if that makes any difference. Of the examples I...
4
by: SHEBERT | last post by:
Here is an example of a SortedList that works as a datasource to the ComboBox and a generic SortedList<that does not works as a datasource to the ComboBox. Why? If I use List and generic List<>,...
4
by: aeshiels | last post by:
Hello, I have a SortedList defined as... SortedList<CUser, CUseruserList = new SortedList<CUser, CUser>(); ....and which to copy it to another sorted list SortedList<CUser,...
1
by: raylopez99 | last post by:
I seem to get name collision between the Generic collection SortedList and C++.NET Framework collection SortedList. How to resolve? Here are the libraries that seem to clash:...
6
by: n3tx | last post by:
Hi! I have a problem with sortedlist, i guess i dont understand how it works. I have a method called GetPublishingPlaces that returns an IList<PublishingPlace> (ex. contains 11 rows) I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.