> If I use Collections.sort(arraylist), the elements in the arraylist will be
> sorted by natural order. What happens when the arraylist is actually an
> arraylist of arraylists - how can you sort against an object?
"natural order" requires that the to-be-sorted elements in the List have
the Comparable interface implemented. If you use ArrayLists as elements
in the List you are out of luck, since ArrayList does not implement the
interface. Prepare for getting a ClassCastException.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Roedy Green - 07 Dec 2005 09:46 GMT
On Wed, 07 Dec 2005 10:12:29 +0100, Thomas Weidenfeller
<nobody@ericsson.invalid> wrote, quoted or indirectly quoted someone
who said :
>If you use ArrayLists as elements
>in the List you are out of luck, since ArrayList does not implement the
>interface. Prepare for getting a ClassCastException.
you would have to extend your ArrayList and have it implement
Comparable and write some Comparators.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Viator - 07 Dec 2005 09:46 GMT
Or extend ArrayList to impelemnt Comparable interface to get the
desired behavior.
Amit :-)
Thomas Hawtin - 07 Dec 2005 17:58 GMT
> "natural order" requires that the to-be-sorted elements in the List have
> the Comparable interface implemented. If you use ArrayLists as elements
> in the List you are out of luck, since ArrayList does not implement the
> interface. Prepare for getting a ClassCastException.
Or compilation errors in current Java.
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
On Wed, 07 Dec 2005 08:25:18 GMT, "Bruce Lee"
<blah@blahbllbllahblah.com> wrote, quoted or indirectly quoted someone
who said :
>If I use Collections.sort(arraylist), the elements in the arraylist will be
>sorted by natural order. What happens when the arraylist is actually an
>arraylist of arraylists - how can you sort against an object?
you need to write a Comparator that compares entire ArrayLists in some
way e..g by number of elements.
see http://mindprod.com/jgloss/comparator.html

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.