Click here to Skip to main content
15,897,518 members

Removing items when a .NET SortedSet goes out of order...

Lee Reid asked:

Open original thread
hi everybody,

does anybody know of a way to remove an item from a .NET sortedset when it falls out of order?

Before you jump to tell me that a sorted set cannot fall out of order, all that needs to happen is for an object in the sorted set to have a parameter (by which the set is sorted) altered.

For example, imagine if we have a Person class with a modifiable 'Name' field, and a sortedset<person> that contains many people, and is sorted by Name.
[0]"April"
[1]"Bob"
[2]"Mary"
[3]"Zelda"

If we change the name of item 0 (April) to "Zoe", it will remain at position 0:
[0]"Zoe"
[1]"Bob"
[2]"Mary"
[3]"Zelda"

What I have tried:
I tried to get the items to fire an event so that when these fields changed ( e.g. Name), the sortedset would find the item, remove it, and then reinsert it, So it should be in the right order. This doesn't work, though, because the sortedset cannot find/remove items when it is out of order: presumably its binary search method stops working. I can't find a method which lets you remove items from a specific index.
I have also tried the RemoveWhere() method like so, but it still doesn't find the items:
C#
public void RemoveSafe(T removeMe)
{
    //removes an item, even if it is out of sort order
    RemoveWhere(a => a == removeMe);
}

an alternate solution, of course, is to remove an item, modify it, then add it back in. Unfortunately my program & sorting criteria is a bazillion times more complex than the Person example above, and this solution would make it very messy indeed...

So, to reiterate:
does anybody know of a way to remove an item from a sorted set when it falls out of order?

Other ideas also welcome :)
Thanks!
Tags: C# (C# 4.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900