Results 1 to 11 of 11
  1. #1
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Wrong sorting of Date list based on current time

    Hi,

    I tried to make a method which sorts a Date list based on current time. (closest time would be the first element of the list). here's the code. But the sorting doesn't worl correct and with every execution the order will change too. apparantly there's a logic error.

    This is the method:

    Java Code:
     private static void sortListByCurrentDate(List < Date > dates) {
    
    
      Collections.sort(dates, new Comparator < Date > () {
       @Override
       public int compare(Date lhs, Date rhs) {
    
        Date cal1 = Calendar.getInstance().getTime();
        lhs = cal1;
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(rhs);
    
        long now = cal1.getTime();
        long time = cal2.getTime().getTime();
    
        if (now < time)
         return -1;
        else if (now == time)
         return 0;
    
        else return 1;
    
    
       }
      });
    
     }
    And this is a test class HelloWorld which I created fo test: (Test the below code online)

    Java Code:
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    
    public class HelloWorld {
     static Date now = Calendar.getInstance().getTime();
    
     public static void main(String[] args) {
    
    
      Calendar c = Calendar.getInstance();
      List < Date > list = new ArrayList < > ();
      System.out.println("Now: " + now + "\n");
    
      for (int i = 0; i <= 4; i++) {
       c.add(Calendar.HOUR, 1);
       Date t = c.getTime();
       list.add(t);
    
      }
      Collections.shuffle(list);
    
      sortListByCurrentDate(list);
    
    
      for (Date d: list) {
       System.out.println("next time: " + d);
      }
    
     }
    
    
    
     private static void sortListByCurrentDate(List < Date > dates) {
    
    
      Collections.sort(dates, new Comparator < Date > () {
       @Override
       public int compare(Date lhs, Date rhs) {
    
        Date cal1 = Calendar.getInstance().getTime();
        lhs = cal1;
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(rhs);
    
        long now = cal1.getTime();
        long time = cal2.getTime().getTime();
    
        if (now < time)
         return -1;
        else if (now == time)
         return 0;
    
        else return 1;
    
    
       }
      });
    
     }
    }
    Where is the problem?

    Thanks
    Last edited by ghandil; 03-01-2016 at 04:47 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Wrong sorting of Date list based on current time

    Your compare routine is supposed to compare the two Date objects that are passed to the method. Why are you creating new instances?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Wrong sorting of Date list based on current time

    Thanks Jim,

    Yes you are right. Then it should be like this I think:

    Java Code:
    private static void sortListByCurrentDate(List < Date > dates) {
     
     
     Collections.sort(dates, new Comparator < Date > () {
      @Override
      public int compare(Date lhs, Date rhs) {
     
       // Two date objects are obtained from method's argumants.
       // Date cal1 = Calendar.getInstance().getTime();
       // lhs = cal1;
    
     
       long now = lhs.getTime();
       long time = rhs.getTime();
     
       if (now < time)
        return -1;
       else if (now == time)
        return 0;
     
       else return 1;
     
     
      }
     });
     
    }
    But how to declare they should be sorted due to their distances of current time, not only compared to each other. where should I pass the current time to the method?

    Thanks

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Wrong sorting of Date list based on current time

    ... and as far as I know, the Date class implements the Comparable<Date> interface; so why all the hoopla with longs and Calendars?

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Wrong sorting of Date list based on current time

    It's the "distance from now" part.

    So if dateA is 1 hour before tNow, and dateB is 30 minutes after tNow, I'm guessing that the order should be:
    dateB
    dateA
    as dateB is "closer" to tNow.

    So the Comparator needs to take tNow and subtract it from both times, and comparing their absolute values.
    I think.

    Though I can see an obvious issue with tNow being different on each call.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ghandil is offline Member
    Join Date
    Jun 2013
    Posts
    28
    Rep Power
    0

    Default Re: Wrong sorting of Date list based on current time

    Quote Originally Posted by JosAH View Post
    ... and as far as I know, the Date class implements the Comparable<Date> interface; so why all the hoopla with longs and Calendars?

    kind regards,

    Jos
    That's true. My thoughts were about what Tolls said:


    Quote Originally Posted by Tolls View Post
    It's the "distance from now" part.

    So if dateA is 1 hour before tNow, and dateB is 30 minutes after tNow, I'm guessing that the order should be:
    dateB
    dateA
    as dateB is "closer" to tNow.

    So the Comparator needs to take tNow and subtract it from both times, and comparing their absolute values.
    I think.

    Though I can see an obvious issue with tNow being different on each call.
    But now I'm thinking the whole concept of caring about past times and next times while sorting is wrong.
    I should just sort the list (even as simple as Collections.sort(list);) and then distinguish the before and after times with a loop. (maybe save them to new lists).


    However apart the functionality, this is still an interesting case:

    So the Comparator needs to take tNow and subtract it from both times, and comparing their absolute values.
    Last edited by ghandil; 03-01-2016 at 06:31 PM.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Wrong sorting of Date list based on current time

    If dateA is 30 minutes before now and dateB is 30 minutes after now, which is closer to now? Or can they be put in any order?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Wrong sorting of Date list based on current time

    Sigh, stick those dates in a SortedSet<Date> and take the tailset(now) from it; as I set, there's no need for all the hoopla ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Wrong sorting of Date list based on current time

    The tailset won't give you an ordered list of the dates based on the given criteria.
    It'll only give the dates after now.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Wrong sorting of Date list based on current time

    Quote Originally Posted by Tolls View Post
    The tailset won't give you an ordered list of the dates based on the given criteria.
    It'll only give the dates after now.
    The tailSet() method returns a SortedSet ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Wrong sorting of Date list based on current time

    But only for the values that are greater than the time entered.
    At least according to the API.
    So any date prior to that will not be in the set (sorry, said list before).

    Of course, that's based on the idea that my interpretation was correct, which it sounds like it may not be. Which means the sorting is simply date order (possibly reversed?).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. converting string (GMT date) to date in US time.
    By JRuyechan in forum New To Java
    Replies: 1
    Last Post: 10-15-2010, 07:07 AM
  2. Assigning values to an object
    By camper2 in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 03:13 AM
  3. Replies: 2
    Last Post: 12-03-2008, 11:14 AM
  4. Creating a Gregorian Calendar using a Date object gives date - 1
    By prachi_goliwadekar in forum New To Java
    Replies: 1
    Last Post: 05-08-2008, 08:32 PM
  5. Replies: 1
    Last Post: 08-06-2007, 11:10 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •