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 Organizing Data Interfaces

Can someone please explain to me this code ?

Can someone please explain the 'compareTo' method thoroughly? How does the "other" object access the other mCreationDate? How does the computer differentiate between the two mCreationDates by putting "other" in front of one of them?

package com.teamtreehouse;
import java.util.Date;


public class Treet {
  private String mAuthor;
  private String mDescription;
  private Date mCreationDate;

  public Treet(String author, String description, Date creationDate) {
   mAuthor = author;
    mDescription = description;
    mCreationDate = creationDate;
  }

  @Override
  public String toString(){
   return "Treet:  " + "\" " +  mDescription + "\""  + "\n -" +  mAuthor ; 
  }

  @Override
  public int compareTo(Object obj){
   Treet other = (Treet) obj;
    if(equals(other){
     return 0; 
    }
       int dtCmp = mCreationDate.compareTo(other.mCreationDate);
       if(dtCmp == 0){
        return mDecription.compareTo(other.mDescription); 
       }
  }

  public String getAuthor()  {
   return mAuthor; 
  }

  public String getDescription() {
   return mDescription; 
  }

  public Date getDate(){
   return mCreationDate; 
  }

  public String[] getWords(){
   return mDescription.toLowerCase().split("[^\\w!]"); 
  }
}

2 Answers

Evgeniia Vakarina
Evgeniia Vakarina
3,317 Points

Ok, first of all the code won't compile at least because there is no second ")" in the line ""if(equals(other){"" , it should be ""if(equals(other)){"". And because there is no "implements Comparable" in the top. Maybe there are some more that I missed =) I'm not as good as the compiler. If to fix all of this and speak about logic - Treet is a class, and it has methods. And one of them is "compareTo()"), another is "equals()" (you don't specifically write it, but it's already inherited from Object class). So when and how we call the methods if they are not static? (and both of those are not) Right, on an instance of the class. You can say that there is no instance there, but there is. It's "this." When you create an instance of Treet like : Treet treet = new Treet(...); every non static method of this class will know that they can be called on this treet. There is also the magic word "this." that can be used for clarity (but it's not obligatory) - try "if(this.equals(other)){" - it should also work. I hope that helps =) If you're still confused, ask me more, and we'll try to figure it out together

Thanks!

4epenaxa com
4epenaxa com
3,613 Points

i have one question. When we do this in Treet.java @Override public int compareTo(Object obj) {.... ...}

this change logic in Example.java for Arrays.sort(treets); ?or where our treets sorted by date?)))

Evgeniia Vakarina
Evgeniia Vakarina
3,317 Points

As I understand Arrays.sort() takes the passed object's compareTo() result. So actual sorting occurs in Arrays.sort (which I guess we can't change, it's Arrays static method), but on which information to sort - that we say in our object's (in this case Treet) compareTo().

Привет из Новосибирска =)

4epenaxa com
4epenaxa com
3,613 Points

Thank you for fast response. Привет из Сочи ;)