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 trialMarc Graci
1,940 PointsQuestion about compareTo
I understand what compareTo method returns. if X > Y, return 1. if X == Y, return 0. if X < Y, return -1.
So I wrote a code like this.
@Override public int compareTo(Object obj) { BlogPost other = (BlogPost) obj; if(equals(other)){ return 0; } if(this.mCreationDate > other.mCreationDate){ return 1; }else{ return -1 } }
This code didn't work.
So I checked the collect code.However, I don't see 1 or -1 in the code. How does it know when to return 1 or -1?
Thank you.
package com.example;
import java.util.Date;
public class BlogPost implements Comparable {
private String mAuthor;
private String mTitle;
private String mBody;
private String mCategory;
private Date mCreationDate;
public BlogPost(String author, String title, String body, String category, Date creationDate) {
mAuthor = author;
mTitle = title;
mBody = body;
mCategory = category;
mCreationDate = creationDate;
}
@Override
public int compareTo(Object obj) {
BlogPost another = (BlogPost) obj;
if(equals(another)){
return 0;
}
int compareDate = mCreationDate.compareTo(another.mCreationDate);
return compareDate;
}
public String[] getWords() {
return mBody.split("\\s+");
}
public String getAuthor() {
return mAuthor;
}
public String getTitle() {
return mTitle;
}
public String getBody() {
return mBody;
}
public String getCategory() {
return mCategory;
}
public Date getCreationDate() {
return mCreationDate;
}
}
3 Answers
Mohammed Yehia
Courses Plus Student 9,255 Pointshere when you used compareTo in this line,
int compareDate = mCreationDate.compareTo(another.mCreationDate);
you returned to use the original compareTo that implemented by java in the original Date class so this is not the overridden compareTo this is the original one
here is what you used, this is the original implementation from java
public int compareTo(Date var1) {
long var2 = getMillisOf(this);
long var4 = getMillisOf(var1);
return var2 < var4 ? -1 : (var2 == var4 ? 0 : 1);
}
Mohammed Yehia
Courses Plus Student 9,255 PointsSorry I didn't fully understand your question but for compareTo, its programmed in java to return 1, -1 or 0 but if you override it then you do what you want check here for more https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html#compareTo(T)
Marc Graci
1,940 PointsThank you for responding to my question. I really appreciate it. Let me change my question. I overrided compareTo so I can get what I want.
if this and another is equals, it returns 0. Othewise it move on to the next line : int compareDate = mCreationDate.compareTo(another.mCreationDate);
This "compareDate " should recieve 1 or -1, but I did not program to do so. Why does "compareDate " recieve 1 or -1?
Hope my English is explain my question.
Marc Graci
1,940 PointsWow!! Yes, it make sense to look to the type of the object that calls compareTo. Now I finally understood! Again, you have been a big hep. Thank you so much!!!!
Mohammed Yehia
Courses Plus Student 9,255 PointsGlad I could help
Marc Graci
1,940 PointsMarc Graci
1,940 PointsAgain, thank you so much for your help. Wow, I was thinking that was a recursion. Is this how it is for any method I override? I mean, if I call method in the method, it's not a recursion, but I'm calling original one?
Mohammed Yehia
Courses Plus Student 9,255 PointsMohammed Yehia
Courses Plus Student 9,255 PointsIf you want to use recursion you would use "this.compareTo" or compareTo directly to tell the compiler to use the overridden method but in this case here that would gives you an error and it would keep call it self again and again. and to know what method you used you can look to the type of the object that calls compareTo, in the example its mCreationDate and its type is Date so it will call compareTo of the Date class. and glad to help