Richard said:
Okay, I wrote a class that implements the Comparator interface and
includes a compare method. Here is the code for the class:
public class DateVectorComparator implements Comparator
{
public final int compare ( Object a, Object b )
{
return ((Date)a).compareTo((Date)b);
}
}
That's the right general idea.
Then within the code for the main class.....
List newList = sites.subList(0, sites.size()); // create a new list
from vector
Collections.sort(newList, new DateVectorComparator());
There is no point whatsoever to creating newList for that purpose -- a
Vector is already a List, so you can pass the original Vector.
I get a ClassCastException when I run the class at the line
"Collections.sort". This makes sense since the list is a list of
objects of type Object[][] and each element in the array is of a
different type: String, Date, etc.
Right. The Comparator used needs to know and account for the types of
the objects to be compared. If each element of the Vector is of type
Object[][], and the Dates by which you want to sort are at position
[1][2], then the Comparator's compare method would look like this:
public int compare(Object a, Object b) {
Object[][] arrayA = (Object[][]) a;
Object[][] arrayB = (Object[][]) b;
return ((Date) arrayA[1][2]).compareTo(arrayB[1][2]);
}
Notes:
() There is no advantage to be gained by making the Comparator's compare
method final.
() There is little advantage to be gained by casting the argument of
Date.compareTo().
You commented in a different message that you had a solution that runs
without error, but doesn't seem to result in a sorted list. If you post
a complete example then we can probably identify the problem, but
otherwise we have no reliable way to diagnose it. Prose descriptions of
your code are not sufficiently detailed for use in debugging.
John Bollinger
(e-mail address removed)