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.