Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Data Structures - Retired Organizing Data Comparable

Nishay Madhani
Nishay Madhani
15,456 Points

Please Explain??

 public int compareTo(Object obj){
    BlogPost other = (BlogPost) obj;
    if (this.equals(other)){
      obj = (BlogPost)obj;
      return 0;
    }
    int compareDate = mCreationDate.compareTo(other.mCreationDate);
    return compareDate;   
  }

I am Not able to understand why we would use 'other' object and when we call the compareTo meth0d for comapareDate which method does it call the inbuilt one from the Objects Class or the one we define

please help??!!

2 Answers

Nishay Madhani

Basically what's happening is you're comparing the Bogposts based on creationDate.

  • the "other" object is just the Blogpost instance you are comparing it to. The seemingly strange if statement is just to check if the Blogpost that is passed into the compareTo method is equal to the current BlogPost instance. (this is one of the strongly encouraged implementation strategies of the interface Compareable - https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html)

As for your question on which compareTo method is called on creationDate:

  • if mCreationDate is a Date class then it will use the Date class's compareTo method.
  • if mCreationDate is a class we have created (that implements the Comparable interface) it will use the compareTo Method we have implemented.

To help things more clear here is what the compareTo function above is doing:

  1. create a BlogPost inner variable and assign to it a reference to the passed in Object (which in this case is a BlogPost - specified by the cast to BlogPost)
  2. the If statement checks if the BlogPost passed into the compareTo method is equal to the BlogPost whose compareTo method was called. To be honest I'm not sure why it then sets obj equal to obj cast as a BlogPost. This seems unnecessary but I could be wrong. If the condition is satisfied it return zero per the interface specification.
  3. next compareDate is set to the result of comparing the creationDate of both blog posts. This is most likely just calling the Java Date objects built in compareTo method.
  4. the compareDate integer is returned as the result of the compareTo method.

The big thing to take home here is that this is allowing us to sort BlogPosts on the creation date.

Let me know if you need any of this clarified - happy to help.