Sorted List Problem

R

Robert Zurer

Assuming that I have created a strongly typed collection and overridden the
appropriate methods, i.e. this[], Add, Insert etc., so that a sort order is
maintained, it's still very possible for a property of the
'ListMemberObject' which is instrumental in the sort to be modified
unbeknownst to the list thereby defeating the sort.

One way to keep the list sorted is to add a 'ParentList' property to the
ListMemberObject and notify that ParentList to sort itself in every set
method of the list object. That would repeat code everywhere and is
extremely yucky.

I've looked at a few implementations of creating a sorted list, most notably

http://www.codeproject.com/useritems/SortableList.asp

but the problem is not addressed.

In a related vein. If I want to use ArrayList.BinarySearch for the express
purpose of only adding unique values,

public override int Add(object o)
{
int result = -1;
if(o == null)throw new ArgumentNullException();
IComparer idComparer = new IDComparer();

Sort(idComparer);
//if I call this here the application dies a slow and painful death with
a large list
//if I don't call it I'm not absolutely sure my list is sorted properly

int binSrch = this.BinarySearch(idComparer );
if(binSrch < 0)
{
base.Insert(~binSrch, o);
result = ~binSrch;
}
return result;
}

What to do?


TIA

Robert Zurer
 
H

Horatiu Ripa

Why didn't you implement a binary tree (ordered)? Well implemented all your
troubles will dissapear.
 
R

Robert Zurer

Thanks for the suggestion, do you have any links to a good implementation??


Robert Zurer
 
H

Horatiu Ripa

try
http://www.javacommerce.com/tutorial/javanotes/c11/s4.html

You don't have to use that implementation but it gives you an idea.

--
Horatiu Ripa
Software Development Manager
Business Logic Systems LTD
21 Victor Babes str., 1st floor, 3400 Cluj-Napoca, Romania
Phone/Fax: +40 264 590703
Web: www.businesslogic.co.uk

This email (email message and any attachments) is strictly confidential,
possibly privileged and is intended solely for the person or organization to
whom it is addressed. If you are not the intended recipient, you must not
copy, distribute or take any action in reliance on it. If you have received
this email in error, please inform the sender immediately before deleting
it. Business Logic Systems Ltd accepts no responsibility for any advice,
opinion, conclusion or other information contained in this email or arising
from its disclosure.
 
H

Horatiu Ripa

The add, search, getnext methods are trivial to implement - recursively -
and you can get them from there.
But the remove function is tricky and quite nice, and not implemented
there!!!

--
Horatiu Ripa
Software Development Manager
Business Logic Systems LTD
21 Victor Babes str., 1st floor, 3400 Cluj-Napoca, Romania
Phone/Fax: +40 264 590703
Web: www.businesslogic.co.uk

This email (email message and any attachments) is strictly confidential,
possibly privileged and is intended solely for the person or organization to
whom it is addressed. If you are not the intended recipient, you must not
copy, distribute or take any action in reliance on it. If you have received
this email in error, please inform the sender immediately before deleting
it. Business Logic Systems Ltd accepts no responsibility for any advice,
opinion, conclusion or other information contained in this email or arising
from its disclosure.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top