Sorting Arraylist

B

Bryon

I need to sort an ArrayList of objects. I am unable to find a method
for this. DO I need to role my own sorting? Or is there something like
the qsort() function in C of old???

Thanks
 
P

Peter Jausovec

Hi,
You can use Sort () method. (ArrayList arr = new ArrayList (); arr.Sort
(); )
 
J

Jonathan Stowe

Bryon said:
I need to sort an ArrayList of objects. I am unable to find a method
for this. DO I need to role my own sorting? Or is there something like
the qsort() function in C of old???

Depending on how you want to sort the ArrayList you can use the Sort()
method of the ArrayList class, if you want to sort by something other
than the 'value' of the Objects you may need to implement an IComparer
that does the comparisons on the appropriate values derived from the
objects - see my post about sorting files by creation date earlier.

/J\
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
Why not use a sortedlist than instead of the arraylist?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpre
f/html/frlrfsystemcollectionssortedlistclasstopic.asp

I hope this helps?

If the OP really wants a "normal" list, SortedList is a bad choice -
it's really a map, which also keeps the keys sorted. It's very badly
named, unfortunately.

Fortunately, the Sort method on ArrayList is just what the OP wants, I
believe :)
 
C

Cor Ligthert

Fortunately, the Sort method on ArrayList is just what the OP wants, I
believe :)

You can be right, however the OP is writing an Arraylist of objects.

I do not know if those objects contain a single value. I don't think so
because he said he was not able to find a method. When you search on MSDN
with "arraylist sort" you get direct the arraylist sort method.

So I think there should me something more.

However I can be wrong in this.

Cor
 
J

Jon Skeet [C# MVP]

Cor Ligthert said:
You can be right, however the OP is writing an Arraylist of objects.

I do not know if those objects contain a single value.

Why would it matter?
I don't think so
because he said he was not able to find a method. When you search on MSDN
with "arraylist sort" you get direct the arraylist sort method.

It's sometimes easy to miss the obvious...

<snip>
 

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