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

Neil Gordon
Neil Gordon
8,823 Points

not sure about this error Bummer! Expected -1 but received 1, when comparing Sun Jul 20 20:18:00 UTC 1969 to Mon Mar 09

looking for assistance on this error

com/example/BlogPost.java
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;
  }
public int compareTo(Object obj){
 int  compareDate = mCreationDate.compareTo(mCreationDate);

  BlogPost bp = (BlogPost) obj; 

  if (equals(bp)) {
    return 0; 

  }
    return 1; 


}
  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;
  }
}

4 Answers

Looking at your error, you're on the last step! Apologies!! You need to compare the objects first; you've done that, then put them in order:

  public int compareTo(Object obj){
    BlogPost bp = (BlogPost) obj;
    if (bp.equals(this)){
      return 0;
    }
    if(this.mCreationDate.before(bp.mCreationDate)){
      return -1; 
    }
    return 1;
  }

You do that by comparing their mCreationDate and returning the appropriate alue, as above.

Sorry for the confusion - I hope this sorts you out!!

Steve.

Hi Neil,

Which stage are you up to on the challenge? I'm assuming you're on 2 of 3?

What error are you getting? If you click through to "Preview" what hint do you get in there as to what failed?

I just copied your code into the challenge and it passed, so I'm not sure what to make of that! (Hence the first question!)

Steve.

Hi Neil,

You're on the right lines. My solution isn't particularly elegant - I am sure there are better!:

  public int compareTo(Object obj){
    BlogPost bp = (BlogPost) obj;
    if(bp.equals(this)){
      return 0;
    }
    return 1;
  }

You could equally use this.equals(bp), of course.

I hope that helps.

Steve.

Neil Gordon
Neil Gordon
8,823 Points

Steve thank you but I am still getting the same error

 public int compareTo(Object obj) {
    BlogPost bp= (BlogPost) obj; 
      if(bp.equals(this)) {
     return 0;
    }
      return 1; 
    }```
Neil Gordon
Neil Gordon
8,823 Points

Steve Thank you I am on the last stage, i made a mod. to the code , forgot to copy before hit check work but it went through the last post was a big help. I had to rearrange my variables