HOW TO SORT ARRAYLIST WITH CREATED CLASS.

Discussion in 'Java' started by justineee, Nov 11, 2008.

  1. justineee

    justineee Guest

    Hey,

    I have this ArrayList..

    ArrayList<Word> dictionary = new ArrayList<Word>();

    I am trying to sort this out but Collections.sort won't work because
    of the "Word" class.
    How will I fix this?

    Thanks

    --
    Justine
     
    justineee, Nov 11, 2008
    #1
    1. Advertising

  2. justineee

    Lew Guest

    justineee wrote:
    > Hey,
    >
    > I have this ArrayList..
    >
    > ArrayList<Word> dictionary = new ArrayList<Word>();
    >
    > I am trying to sort this out but Collections.sort won't work because
    > of the "Word" class.
    > How will I fix this?


    By reading the Javadocs for Collections.sort(), of course.

    Did you try that?

    <http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)>
    > Sorts the specified list into ascending order,
    > according to the natural ordering of its elements.


    This one requires that you define a natural ordering of Word elements. Did
    you do that?

    <http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)>
    > Sorts the specified list according to the order
    > induced by the specified comparator.


    This one requires that you define a Comparator <? super Word>. Did you do that?

    Have you gotten around to defining equals() and hashCode() for Word?

    --
    Lew
     
    Lew, Nov 11, 2008
    #2
    1. Advertising

  3. justineee

    Nigel Wade Guest

    justineee wrote:

    > Hey,
    >
    > I have this ArrayList..
    >
    > ArrayList<Word> dictionary = new ArrayList<Word>();
    >
    > I am trying to sort this out but Collections.sort won't work because
    > of the "Word" class.
    > How will I fix this?
    >
    > Thanks


    Collections.sort() can only sort objects which have a specific ordering
    otherwise it cannot know what the order is. This requires either the class to
    be Comparable or for you to supply a Comparitor for the class.

    If you read the API for Collections.sort(List) you'll see it is defined as:
    public static <T extends Comparable<? super T>> void sort(List<T> list)
    which means the the class T (the class the List contains) must be Comparable.

    The other sort method is:
    public static <T> void sort(List<T> list,
    Comparator<? super T> c)

    this can be used when T is not Comparable, but you must supply a Comparator for
    T.

    See
    http://java.sun.com/docs/books/tutorial/collections/algorithms/index.html#sorting
    for more detailed information.


    --
    Nigel Wade
     
    Nigel Wade, Nov 11, 2008
    #3
  4. > I have this ArrayList..
    >
    > ArrayList<Word> dictionary = new ArrayList<Word>();
    >
    > I am trying to sort this out but Collections.sort won't work because
    > of the "Word" class.
    > How will I fix this?
    >
    > I have this ArrayList..
    >
    > ArrayList<Word> dictionary = new ArrayList<Word>();
    >


    Your Word class must implement the Comparable interface and implement the
    method compareTo(Object obj){}
    Like this:

    public class Word implements Comparable{
    ....
    public int compareTo(Word w) {
    return this.word.compareTo(w.getWord());
    }
    }

    then it's possible to sort the list :)

    - Brian
     
    Brian Odsgaard, Nov 11, 2008
    #4
  5. > public class Word implements Comparable{
    > ...
    > public int compareTo(Word w) {
    > return this.word.compareTo(w.getWord());
    > }
    > }
    >
    > then it's possible to sort the list :)
    >
    > - Brian
    >


    Small correction - the implemention should looke like this:

    public class Word implements Compareble<Word>{
    ....
    public int compareTo(Word w) {
    return this.word.compareTo(w.getWord());
    }
    }
     
    Brian Odsgaard, Nov 11, 2008
    #5
    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,741
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,382
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,375
  4. Philipp
    Replies:
    6
    Views:
    927
    Arne Vajhøj
    May 28, 2008
  5. Navin
    Replies:
    1
    Views:
    698
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page