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]
# 1
The problem is that Stroke doesn't implement Comparable.Kaj
kajbja at 2007-7-11 15:51:19 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

DrLaszloJamfa at 2007-7-11 15:51:19 > top of Java-index,Java Essentials,Java Programming...
# 3
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.
Symb0la at 2007-7-11 15:51:19 > top of Java-index,Java Essentials,Java Programming...
# 4
Earlier you mentioned that you are displaying information in a JTable.If you are using the current version of Java (Java SE 6), there is built-insupport for sorting and filtering: http://java.sun.com/developer/JDCTechTips/2005/tt1115.html#2
DrLaszloJamfa at 2007-7-11 15:51:19 > top of Java-index,Java Essentials,Java Programming...