comparable objects

S

shawn

I want to make an object that holds an event description and a date for
that event. This object needs to be sorted, by date, into group of
others that are the same type once it's created, making a scheduler
kind of thing. How can I make it comparable by the GregorianCalender's
date that each contains. I need to use a binary search tree structure
to organize the events, which is why they need to be comparable by the
dates.
 
V

VisionSet

shawn said:
I want to make an object that holds an event description and a date for
that event. This object needs to be sorted, by date, into group of
others that are the same type once it's created, making a scheduler
kind of thing. How can I make it comparable by the GregorianCalender's
date that each contains. I need to use a binary search tree structure
to organize the events, which is why they need to be comparable by the
dates.

Why do you need GregorianCalendar?

class YourClass implements Comparable {

public int compareTo(Object obj) {

...//check instanceof YourClass

YourClass that = (YourClass)obj;

...// deal with nulls appropriately

return this.yourDateAttribute.compareTo(that.yourDateAttribute);
}
}
 
S

shawn

Thanks for the help, I'm working on this project in my comp sci class
thats due in two days. I havent done much with Comparable in a long
time and I just couldnt remember how to use it.
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,040
Latest member
papereejit

Latest Threads

Top