Sortable collection

D

_DS

I understand that the usual way of creating a strongly typed
collection is to inherit from CollectionBase. However, CollectionBase
does not support sorting. What is the best way to implement this?
Derive from ArrayList?
 
J

Jon Skeet [C# MVP]

_DS said:
I understand that the usual way of creating a strongly typed
collection is to inherit from CollectionBase. However, CollectionBase
does not support sorting. What is the best way to implement this?
Derive from ArrayList?

CollectionBase.InnerList returns an ArrayList, so you can sort that.

Of course, in 2.0 you probably wouldn't use CollectionBase in the first
place, but List<T>.
 
N

Nick Hounsome

_DS said:
I understand that the usual way of creating a strongly typed
collection is to inherit from CollectionBase. However, CollectionBase
does not support sorting. What is the best way to implement this?
Derive from ArrayList?

I honestly do not understand the thinking process of some people who post on
these groups.

This is how I would think:

1) I want to implement a strongly typed collection so I use CollectionBase
2) What does CollectionBase give me to help implement this? Oh. there's this
protected property called InnerList which gives access to the ArrayList used
to implement the collection.
3) I will implement Sort by calling InnerList.Sort()

I can't see how it could be any easier.
The hardest part is finding CollectionBase in the first place and even that
comes naturally from saying to yourself "I want to do collection stuff so
what is in the Collections namespace?"

I don't like to flame but RTFM dude - 99 times out of a 100 its quicker than
posting and you learn extra stuff beyond your particular problem of the
moment.
 
G

Guest

_DS,

You will probably want to implement IComparable in the class for which you
are building the CollectionBase implementation - not in the collection but in
the class the collection will hold.

And there's nothing that says you couldn't then create your own Sort command
in your custom collection, but John is correct, there is one that you can use.

In either case, writing your own sort or using InnerList.Sort(), you should
implement IComparable so that the sort can do the comparisons that are
required.

HTH
--
Dale Preston
MCAD C#
MCSE, MCDBA
 
N

Nick Hounsome

One RTFM at the end of a post containing the answer to his question plus the
constructive advice about how to go about answering future questions hardly
constitutes motivation by insult.

Surely you must agree that to post without even looking up the members of
the class you are using indicates either laziness or poor problem solving
skills.
 
D

_DS

I honestly do not understand the thinking process of some people who post on
these groups.

Here's my thinking process: You should find those guys who beat you
up on the playground and give them a good scolding. I do want to
thank you for my heightened appreciation for the Dale Prestons and
John Skeets of the world. Usenet would be a sorry place...

Dale and John, thanks again. I'll eventually move to generics, but I
had already written a class that derived from ArrayList and
implemented compare functions, etc. I was going to change to derive
from CollectionBase instead. I did miss the InnerList property,
partly as I was looking for functions. But more because I thought
that CollectionBase was further down in the inheritance hierarchy and
that ArrayList was derived from CollectionBase. I'll check into that.
 
N

Nick Hounsome

_DS said:
Here's my thinking process: You should find those guys who beat you
up on the playground and give them a good scolding. I do want to
thank you for my heightened appreciation for the Dale Prestons and
John Skeets of the world. Usenet would be a sorry place...

Hypocrit! This is way more of a flame than I dished out.
Dale and John, thanks again. I'll eventually move to generics, but I
had already written a class that derived from ArrayList and
implemented compare functions, etc. I was going to change to derive
from CollectionBase instead. I did miss the InnerList property,
partly as I was looking for functions.

Hopefully thanks to my comment you have now learned to look for protected
properties.
But more because I thought
that CollectionBase was further down in the inheritance hierarchy and
that ArrayList was derived from CollectionBase.

Why? Inheritance is shown quite clearly near the top of the doc page.

You are just proving my point that a few moments careful reading of the
documentation about a class is more time efficient than posting.
 
D

_DS

Hypocrit! This is way more of a flame than I dished out.

More of a flame than your "Read The ****ing Manual" rant? (or did you
not know what RTFM means). You can dish it out, I'll give you that.
Hopefully thanks to my comment you have now learned to look for protected
properties.

Hopefully you'll try decaf. In the future, consider what you're
saying and please don't 'Help" me any more. I thought this group was
relatively free of flamers.

DS
 
B

Bill Butler

_DS said:
More of a flame than your "Read The ****ing Manual" rant? (or did you
not know what RTFM means). You can dish it out, I'll give you that.

Perhaps he meant "Read the FINE Manual" and you just took it wrong?!?!

:^)

OK...I'll go away now

Bill
 

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