469,329 Members | 1,453 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,329 developers. It's quick & easy.

Sorted List Problem

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

Nov 15 '05 #1
4 2324
Why didn't you implement a binary tree (ordered)? Well implemented all your
troubles will dissapear.
--
Horatiu Ripa

"Robert Zurer" <ro****@zurer.com> wrote in message
news:uB**************@TK2MSFTNGP10.phx.gbl...
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

Nov 15 '05 #2
Thanks for the suggestion, do you have any links to a good implementation??
Robert Zurer
"Horatiu Ripa" <ho**********@businesslogic.co.uk> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...
Why didn't you implement a binary tree (ordered)? Well implemented all your troubles will dissapear.
--

Nov 15 '05 #3
try
http://www.javacommerce.com/tutorial...es/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.

"Robert Zurer" <ro****@zurer.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
Thanks for the suggestion, do you have any links to a good implementation??

Robert Zurer
"Horatiu Ripa" <ho**********@businesslogic.co.uk> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...
Why didn't you implement a binary tree (ordered)? Well implemented all

your
troubles will dissapear.
--


Nov 15 '05 #4
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.

"Robert Zurer" <ro****@zurer.com> wrote in message
news:uA**************@TK2MSFTNGP11.phx.gbl...
Thanks for the suggestion, do you have any links to a good implementation??

Robert Zurer
"Horatiu Ripa" <ho**********@businesslogic.co.uk> wrote in message
news:u9**************@tk2msftngp13.phx.gbl...
Why didn't you implement a binary tree (ordered)? Well implemented all

your
troubles will dissapear.
--


Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

57 posts views Thread by Egor Bolonev | last post: by
10 posts views Thread by Der Andere | last post: by
3 posts views Thread by Andrew Clark | last post: by
1 post views Thread by Khayrat | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.