8 Replies Latest reply: Jun 30, 2008 10:13 AM by 807589 RSS

    Help with Timestamp's compareTo

    807589
      Greetings,

      I've written some code and it doesn't work as I expect it to be. Very strange and I'm not sure why. Please help with this.

      I need to sort a result set "descending". It cannot be done at the SQL level b/c the SQL uses some aggregate functions and I cannot use "order by <column> desc", so I decided to write a class which implements Comparator. This is what I've written for my class:

      Very simple. It compares two Object's and returns the value of Timestamp's compareTo() method.
      My expectation is that time1.compareTo(time2) should sort the list ascending and time2.compareTo(time1) should sort descending. Please correct me here if I'm wrong. But in both cases, the list is sorted ascending !!

      ----------------------------------------------------------------------------------------
      public class AdjListComparator implements Comparator {
      /* (non-Javadoc)
      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
      */
      public int compare(AdjReportLine line1, AdjReportLine line2) {
      // TODO Auto-generated method stub
      // 2007-05-15 13:28:35.686
      try {
      SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
      java.util.Date date1 = sdf1.parse(line1.getPlacedAt());
      java.sql.Timestamp time1 = new java.sql.Timestamp(date1.getTime());

      SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss.SSS");
      java.util.Date date2 = sdf2.parse(line2.getPlacedAt());
      java.sql.Timestamp time2 = new java.sql.Timestamp(date2.getTime());

      //return time1.compareTo(time2);
      return time2.compareTo(time1); // time2 - time1 to sort descending...
      }
      catch(ParseException pe) {
      return 0;
      }
      }

      /* (non-Javadoc)
      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
      */
      public int compare(Object o1, Object o2) {
      // TODO Auto-generated method stub
      return 0;
      }
      }
      ----------------------------------------------------------------------------------------

      And the caller line from a different class:

      Collections.sort(adjList, new AdjListComparator());

      The code compiles fine. Just not sorting the list descending based on the value of getPlacedAt().

      Much appreciated with help.

      Mahdad
        • 1. Re: Help with Timestamp's compareTo
          800472
          mahdad1 wrote:
          Greetings,

          I've written some code and it doesn't work as I expect it to be. Very strange and I'm not sure why. Please help with this.

          I need to sort a result set "descending". It cannot be done at the SQL level b/c the SQL uses some aggregate functions and I cannot use "order by <column> desc"
          Why can't you:

          a) Select your results into a temporary table, then select from that with order?
          b) Enclose your existing query with a query that uses the proper ordering?
          • 2. Re: Help with Timestamp's compareTo
            807589
            Thanks for suggesting this. Yes, that's one option to use a temp table and use more SQL tricks. But, I was thinking why this cannot be done at Java level?
            Isn't sorting a list descending supported in Java? To me, it's ABC of math: If a function returns the comparison of a and b, then the function comparing b and a should be the negative of the original one. It seems Java doesn't support this. Or it does and the code is incorrect.

            Mahdad
            • 3. Re: Help with Timestamp's compareTo
              807589
              Or it does and the code is incorrect.
              That's most likely the ticket.

              You do a lot of (it seems to me) unnecessary gyrations in that compare method.

              A few of the glaring
              1. You don't need multiple SimpleDateFormatters.
              2. You should not create a new one for every compare call.
              3. Do you even need to do this conversion??
              Based on the format string, it appears as if you could simply sort the string version of the timestamp.

              All of that aside, you should ensure that you are actually comparing the time that you think you are.

              Edited by: johndjr on Jun 30, 2008 10:40 AM
              • 4. Re: Help with Timestamp's compareTo
                795426
                mahdad1 wrote:
                Greetings,
                I need to sort a result set "descending". It cannot be done at the SQL level b/c the SQL uses some aggregate functions and I cannot use "order by <column> desc", so I decided to write a class which implements Comparator. This is what I've written for my class:
                That's a rather arbitrary restriction, but whatever.

                I see two major problems with your code. First, Comparator is a generic class as of Java 5. You should be extending Comparator<Foo>.
                Because you extended the raw Comparator type, your first compare() method will never be called. The only compare method anything that uses a raw Comparator knows about is compare(Object, Object), which you defined to return 0.

                You didn't even try adding println statements into those methods to see which was being called, did you?
                • 5. Re: Help with Timestamp's compareTo
                  807589
                  Thanks for all the responses and suggestions. I had a similar idea that my method is never called. I'll check myself.
                  • 6. Re: Help with Timestamp's compareTo
                    807589
                    Thanks again for all the suggestions. I put the logic into the Object version of compare method. I should not provide a compare method myself. It works now, sorts the list descending. The logic was never called and I did some cleanup too.
                    • 7. Re: Help with Timestamp's compareTo
                      800472
                      mahdad1 wrote:
                      Thanks for suggesting this. Yes, that's one option to use a temp table and use more SQL tricks. But, I was thinking why this cannot be done at Java level?
                      I'm not sure that a subquery is a fair definition of a "trick". Databases are great at this sort of thing, and you should take advantage of that to let your database present data to your client in a way that is sensible and easy to work with. It'll make it faster and produce code that is more maintainable.
                      • 8. Re: Help with Timestamp's compareTo
                        807589
                        No doubt. But due to the complexity of the query, I'd rather keep it intact and sort it in Java. Thanks.