Hello,
i am looking for an easy way to do a "RemoveAll(Pred icate<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 4 4310
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 RemoveAllFromSo rtedList<TKey, TValue>(SortedL ist<TKey,
TValuesortedLis t,
Predicate<KeyVa luePair<TKey, TValue>match)
{
// The number of items removed.
int itemsRemoved = 0;
// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<T Key, TValuepair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remo ve(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.co m
"Michael Nesslinger" <Mi************ ****@gmx.dewrot e in message
news:46******** @news.kullen.rw th-aachen.de...
Hello,
i am looking for an easy way to do a "RemoveAll(Pred icate<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
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 RemoveAllFromSo rtedList<TKey, TValue>(SortedL ist<TKey,
TValuesortedLis t,
Predicate<KeyVa luePair<TKey, TValue>match)
{
// The number of items removed.
int itemsRemoved = 0;
// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<T Key, TValuepair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remo ve(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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** **********@TK2M SFTNGP06.phx.gb l...
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 RemoveAllFromSo rtedList<TKey, TValue>(SortedL ist<TKey,
TValuesortedLis t,
Predicate<KeyVa luePair<TKey, TValue>match)
{
// The number of items removed.
int itemsRemoved = 0;
// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<T Key, TValuepair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remo ve(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.co m
"Michael Nesslinger" <Mi************ ****@gmx.dewrot e in message
news:46******** @news.kullen.rw th-aachen.de...
>Hello,
i am looking for an easy way to do a "RemoveAll(Pred icate<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
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** **********@TK2M SFTNGP06.phx.gb l...
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 RemoveAllFromSo rtedList<TKey, TValue>(SortedL ist<TKey,
TValuesortedLis t,
Predicate<KeyVa luePair<TKey, TValue>match)
{
// The number of items removed.
int itemsRemoved = 0;
// Cycle through each of the values in the sorted list.
foreach (KeyValuePair<T Key, TValuepair in sortedList)
{
// If the predicate returns true, then remove the item.
if (match(pair))
{
// Remove the item.
sortedList.Remo ve(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.co m
"Michael Nesslinger" <Mi************ ****@gmx.dewrot e in message
news:46******** @news.kullen.rw th-aachen.de...
>Hello,
i am looking for an easy way to do a "RemoveAll(Pred icate<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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 key-value pairs
of UserID - DisplayName for a whole bunch of user objects. The UserID
is simply a long, the DisplayName is something like, "Jones,...
|
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 integers starting
with 1. For some reason this is not the result.
IDictionaryEnumerator dictEnum = sortedList.GetEnumerator();
int i=1;
while...
|
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 their new values:
System.Web.HttpContext.Current.Application.RemoveAll();
Is this a safe thing to do? Is it possible that RemoveAll will remove...
|
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().
I add other sorted lists to DataList with the following code:
internal void addData(System.Collections.SortedList inList) {
...
|
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 have seen it looks like the Sorted List does not use
the ^ while other variables do use the ^. I have used the SortedList
with the ^ and gcnew and it...
| |
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<>, both works.
private void Form1_Load(object sender, EventArgs e)
{
System.Collections.SortedList QA1 = new
System.Collections.SortedList();
|
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, CUseruserList2 = new SortedList<CUser,
|
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:
System::Collections::SortedList,
System::Collections::Generic::SortedList,
using namespace System::Collections;
using namespace System::Collections::Generic;
...
|
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 to sort the list in different orders, I dont know if this is something that SortedList do ?
i have a property in the PublishingPlace called Place,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |