Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

Collections.sort(arraylist)

Thread view: 
Bruce Lee - 07 Dec 2005 08:25 GMT
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?
Thomas Weidenfeller - 07 Dec 2005 09:12 GMT
> 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/

Roedy Green - 07 Dec 2005 09:45 GMT
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.

Bruce Lee - 07 Dec 2005 10:08 GMT


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.