SortedSet

Hi.
I've got a Vector, containing Appointment objects.
Each Appointment object contains date, start time, end time and description.

eg.
Date       Start Time   End    
21/02/2003   09:00      10:00
21/02/2003   07:00      08:00

I need to sort the appointment according to date, n in the example above, if the dates are the same, i need to sort according to the start time in ascending order.
How can i implement this using SortedSet?

Thank you.
danadannyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Can you post the class Appointment?
0
shji1Commented:
I would try using the java.util.Date class in order to hold the date and the start time.
You can create a Date object by using:
Date d = DateFormat.getDateInstance().parse("21/02/2002 9:0"); // never tested it...
Or - A better way, in my opinion:
Date d = (new GregorianCalendar(2002,02,21,9,0)).getTime();

Since Date object are Comparables you can easily compare 2 dates, so you can pretty much easily create a Comparator to be used in your SortedSet (or TreeSet).
--Shaji
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shji1Commented:
I would try using the java.util.Date class in order to hold the date and the start time.
You can create a Date object by using:
Date d = DateFormat.getDateInstance().parse("21/02/2002 9:0"); // never tested it...
Or - A better way, in my opinion:
Date d = (new GregorianCalendar(2002,02,21,9,0)).getTime();

Since Date object are Comparables you can easily compare 2 dates, so you can pretty much easily create a Comparator to be used in your SortedSet (or TreeSet).
--Shaji
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

shji1Commented:
I would try using the java.util.Date class in order to hold the date and the start time.
You can create a Date object by using:
Date d = DateFormat.getDateInstance().parse("21/02/2002 9:0"); // never tested it...
Or - A better way, in my opinion:
Date d = (new GregorianCalendar(2002,02,21,9,0)).getTime();

Since Date object are Comparables you can easily compare 2 dates, so you can pretty much easily create a Comparator to be used in your SortedSet (or TreeSet).
--Shaji
0
allahabadCommented:
1. Implement Comparable iterface for Appointment object.

ex.

import java.util.*;
public class Appointment implements Comparable {

   String date;
   String startTime;
   String end;


   public Appointment(String date, String time, String end){

      this.date = date;
      this.startTime = time;
      this.end = end;


   }

   public String toString(){


      return (date + " : " + startTime + " : " + end);
   }

   public int compareTo(Object o){

     
      String objectDate = ((Appointment)o).getDate();
      String objectStartTime = ((Appointment)o).getStartTime();
      String objectEnd = ((Appointment)o).getEnd();


      if (objectDate.compareTo(date) >0) return -1;
      if (objectDate.compareTo(date) <0)return 1;

      if (objectDate.compareTo(date) == 0) {
         if (objectStartTime.compareTo(startTime) >0) return -1;
         if (objectStartTime.compareTo(startTime) <0) return 1;

         if (objectStartTime.compareTo(startTime) ==0) {
            if ( objectEnd.compareTo(end) > 0) return  -1;
            if ( objectEnd.compareTo(end) < 0 ) return 1;

         }

      }

      return 0;



   }

   public String getDate(){
      return date;
   }

   public String getStartTime(){
      return startTime;
   }

   public String getEnd(){
      return end;
   }


}

2. Use TreeSet (SortedSet type) to sort the contents of the
Vector object that holds Appointment objects.

ex.
import java.util.*;

public class SortApointment {


   public static void main(String args[]){


      Vector appList = new Vector();

      appList.add(new Appointment("22/02/2003","06:00","05:00"));
      appList.add(new Appointment("21/10/2003","09:00","10:00"));
      appList.add(new Appointment("21/02/2003","07:00","08:00"));
     

      TreeSet ts = new TreeSet(appList);

      Iterator it = ts.iterator();

      while(it.hasNext()){

         Appointment ap = (Appointment) it.next();
         System.out.println(ap.toString());

      }

   
   }


}
0
functionpointerCommented:
you really should just use 2 java.util.Date objects, not Strings. Which date are you going to use when the appointment starts one minute before midnight and lasts 10 hours? And if it doesnt cross midnight to you, you can be sure it crosses midnight to someone, somewhere.
If you do switch to Dates, you will be able to simply check the .before(), .after() to sort, include simple methods to return the duration of appointment, and use java.util.DateFormat to print accurate times for the user, whereever they may be.
0
CEHJCommented:
You can't implement reliable sorting in this way, as you are sorting the dates as strings, which is not a rational way of going about this. allahabad's code will fail as a result. You *could* decompose the date into its tokens and compare each token, but you'd be wiser following the suggestion of shji1, particularly as the start time is logically connected to the date anyway. The 'end time' should also be held as a Date too. This doesn't, btw, prevent you from also storing the component parts as Strings simultaneously.

This is why i asked you to post the definition of Appointment, as i suspected you are storing the component parts as Strings, and it seems that allahabad also suspects this ;-)
0
CEHJCommented:
Well, functionpointer posted that as i was typing - convinced yet?
0
NetWizeCommented:
This won't work with the Date mentioned above!

Date       Start Time   End    
21/02/2003   09:00      10:00
21/02/2003   09:00      11:00

Keep in mind that your SortedSet can have no duplicate keys (by definition) so you won't be able to store two appointments with same date and starttime if you use the Date with these two values!

I suggest your class should implement the Comparable. It's OK to use the date for compare as long as date and starttime are different if the are equal you may need to take the end-time as additional sorting criteria.

Jan
0
functionpointerCommented:
NetWize brings forth an interesting point...  BUT- unless you are a doctor, lawyer, dentist, or politician, why would you want to schedule multiple appointments at the same time?
Seriously though, some broken down and tired old Java guru once told me, "If you haven't overridden equals() and hashcode(), you haven't finished creating your class...". There is some powerful truth in that. You have to ask yourself if you want to compare actual object instances, or start-end dates, appointment attendees, etc. Dont be afraid to create methods like equalStartTime(Appointment a), or equalEndTime(Appointment a), or equalAttendees(Appointment a).
So you enhance your Appointment class to include a member String[] appointmentAttendees, or a member String appointmentChair and use these to make the appointment unique when you override hashcode(). If you are creating a real scheduling system, these items are an integral part of the uniqueness of the Appointment. How else would you schedule appointments for multiple persons or attendees?
In the case where the application sets appointments for a single user, wouldn't it be nice to know the subject or topic of the meeting?
A shortcut will only lead to unmaintainable and unreusable code. I don't think I have ever heard a client say, "Oh gosh, do this over. This program is too functional." On the other hand, I have heard many a programmer, including myself, say, "Damn... I've done this before... How did I do that one thing? Hmmmm." tick,tock,tick,tock... $ has left the building
 
0
CleanupPingCommented:
danadanny:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
tomboshellCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
Accept shji1's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thomas Boshell
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.