Collections.sort(arraylist)

Discussion in 'Java' started by Bruce Lee, Dec 7, 2005.

  1. Bruce Lee

    Bruce Lee Guest

    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?
     
    Bruce Lee, Dec 7, 2005
    #1
    1. Advertising

  2. Bruce Lee wrote:
    > 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
    --
    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/
     
    Thomas Weidenfeller, Dec 7, 2005
    #2
    1. Advertising

  3. Bruce Lee

    Roedy Green Guest

    On Wed, 07 Dec 2005 08:25:18 GMT, "Bruce Lee"
    <> 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
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 7, 2005
    #3
  4. Bruce Lee

    Roedy Green Guest

    On Wed, 07 Dec 2005 10:12:29 +0100, Thomas Weidenfeller
    <> 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.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Dec 7, 2005
    #4
  5. Bruce Lee

    Viator Guest

    Or extend ArrayList to impelemnt Comparable interface to get the
    desired behavior.

    Amit :)
     
    Viator, Dec 7, 2005
    #5
  6. Bruce Lee

    Bruce Lee Guest

    thanks a lot
     
    Bruce Lee, Dec 7, 2005
    #6
  7. Thomas Weidenfeller wrote:
    >
    > "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
    --
    Unemployed English Java programmer
    http://jroller.com/page/tackline/
     
    Thomas Hawtin, Dec 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,794
    Natty Gur
    Aug 19, 2003
  2. =?Utf-8?B?cm9ubm9jbw==?=

    Collections - ArrayList not referencing correct object

    =?Utf-8?B?cm9ubm9jbw==?=, Jul 12, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    403
    =?Utf-8?B?cm9ubm9jbw==?=
    Jul 12, 2005
  3. Doug Poland
    Replies:
    9
    Views:
    763
    VisionSet
    Sep 27, 2003
  4. mutex
    Replies:
    0
    Views:
    230
    mutex
    Jul 27, 2003
  5. Navin
    Replies:
    1
    Views:
    762
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page