sorting vectors with Date objects

R

Richard Anderson

Hello,

I am looking for input as to the best way to sort a vector which
contains objects of type "X". The type has fields of type String,
java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.

The main goal is for me to write a method that takes a vector of these
object types and sort them by the date field. Does anyone have any
recommendation as to how to solve this issue?

-Richard
 
S

Sudsy

Richard said:
Hello,

I am looking for input as to the best way to sort a vector which
contains objects of type "X". The type has fields of type String,
java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.

The main goal is for me to write a method that takes a vector of these
object types and sort them by the date field. Does anyone have any
recommendation as to how to solve this issue?

-Richard

Collections.sort( Vector, Comparator );

Vector implements the List interface and for the Comparator you just
need a class which overrides the compare( Object o1, Object o2 )
method. Fair enough?
 
R

Roedy Green

I am looking for input as to the best way to sort a vector which
contains objects of type "X". The type has fields of type String,
java.util.Date, and ints. I am limited to using the IBM JDK v. 1.3.

The main goal is for me to write a method that takes a vector of these
object types and sort them by the date field. Does anyone have any
recommendation as to how to solve this issue?

see http://mindprod.com/jgloss/sort.html

I points you to source for various sorts you can use. When did sort
show up in the JDK?
 
N

newszilla

Roedy Green said:
On 15 Dec 2003 16:12:18 -0800, (e-mail address removed) (Richard
Anderson) wrote or quoted :
I points you to source for various sorts you can use. When did sort
show up in the JDK?
Since 1.2, when the Collections class was added

Henk van Voorthuijsen
 
R

Richard Anderson

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

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

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.

Questions, comments, complaints?
 
P

P.Hill

Richard said:
return ((Date)a).compareTo((Date)b);
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.

You need to define a rule for how String sorts in relation Date etc.
That is what the Comparator implementation is for.
Does "Paul" come before "the 16 of December" or after?
yes, of course it can't sort your array, you haven't told it how it.
Think! Computers do what you tell them. You told it
to treat everything as if it were a Date. How do expect it
to do that? It can't.

You might come up with a compare routine that includes
the use of instanceof. Look it up.

Personally I would put something in the list that can
hold each of the types of objects you want to sort, YMMV.

HTH,
-Paul
 
R

Richard Anderson

Okay, so I solved the ClassCastException issue by pulling the date
element out of the array in the compare method within the
DateVectorComparator class. But....the final list is not sorted.
Using the debugger in Eclipse IDE and viewing the variables it looks
like the two objects that are passed into the compare method are
exactly the same (equal). Can anyone provide some insight as to what
the heck is going on or what I could be doing wrong?

-Richard
 
J

John C. Bollinger

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)
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top