Results 1 to 6 of 6
  1. #1
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default Array List Sorting

    I need to sort my arraylist which has some java bean objects.I tried Collections.sort() to do so.But I could not.
    Is there any way to do so?
    Mak
    (Living @ Virtual World)

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makpandian View Post
    I need to sort my arraylist which has some java bean objects.I tried Collections.sort() to do so.But I could not.
    Is there any way to do so?
    Do your beans implement the Comparable<T> interface? (as stated by the API documentation of the Collections.sort( ... ) method).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    441
    Rep Power
    6

    Default

    I did not.Is the reason for that issue?
    Mak
    (Living @ Virtual World)

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,363
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by makpandian View Post
    I did not.Is the reason for that issue?
    You bet; how else is that sorting method supposed to check if a < b or b < a?
    It needs to compare the objects in the collection so these objects need to be comparable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    If you don't want the class to have to implement Comparable you create a Comparator<T> object and use this in the Collections.sort(...) overload that takes a Comparable as the second parameter.

Similar Threads

  1. object list sorting
    By karq in forum Advanced Java
    Replies: 2
    Last Post: 10-08-2010, 05:45 PM
  2. Need help with sorting a linked list
    By SteroidalPsycho in forum New To Java
    Replies: 0
    Last Post: 05-04-2010, 01:15 AM
  3. List Sorting method.
    By bit_bit in forum New To Java
    Replies: 1
    Last Post: 02-24-2010, 11:44 AM
  4. Replies: 5
    Last Post: 02-07-2009, 07:48 AM
  5. Converting array to list and sorting it
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •