HOW TO SORT ARRAYLIST WITH CREATED CLASS.

J

justineee

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
 
L

Lew

justineee said:
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?

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?

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?
 
N

Nigel Wade

justineee said:
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.
 
B

Brian Odsgaard

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
 
B

Brian Odsgaard

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());
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top