Sort ArrayList Problem
Hi,
I am trying to sort my arraylist which is displayed inside a table. I have tried using Collections.sort(strokeList); and get this error msg:
Exception in thread"main" java.lang.ClassCastException: golfSoftwarePackage.Stroke cannot be cast to java.lang.Comparable
at java.util.Collections.sort(Unknown Source)
I know the problem is to do with comparable, well so i think, but i just dont no?!
Please Help.
PS Can i sort the table instead of the arraylist?! Just an idea.
[542 byte] By [
Symb0la] at [2007-11-27 0:00:50]

You can only sort objects that have support for being sorted. Just tossing
arbitrary objects into a list and hoping they can be sorted is magical thinking.
It wouldn't work.
There are two options when you sort a list:
Collections.sort(list);
Collections.sort(list, comparator);
Read the API for both of them, and you will know what to do:
[url=http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List)]sort(list)[/url]
[url=http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)]sort(list, comparator)[/url]
In the first version, all the objects in the list must have type Comparable, implying that
they have the compareTo method, which is used by sort() to do the sorting.
In the second version, the Comparator's compare method is used instead.
So, you should either have Stroke implement Comparable, if there is a natural ordering
to strokes, or write a Comparator otherwise. Either way,
it's usually a very simple task and only a minute's work.
You were dead right stroke did not implement comparable. now it does not error but does not sort, i had to input a compareTo routine so it worked. I no i havent told it to sort anything, so how do I sort my players names? players score? thanks.