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

Justin Barrett
Justin Barrett
27,345 Points

Make sure that your class implements the Comparable method.

I'm completely stuck in this code challenge. Maybe I've been at it too long but I keep getting the "Make sure that your class implements the Comparable method." error and don't know how to fix.

com/example/BlogPost.java
package com.example;
import java.util.Arrays;
import java.util.Date;

public class BlogPost {
  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 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;
  }

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

}

2 Answers

The immediate issue is that you need to have the class implement the interface:

public class BlogPost implements Comparable {

Then, for the method:

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

Note that with Java 7 you would have the class implement Comparable<BlogPost>, and the compareTo() method would be required to accept a BlogPost object, rendering the cast unnecessary.

Justin Barrett
Justin Barrett
27,345 Points

That makes sense. Thank you!

I edited the answer because I forgot to write the less than and greater than signs as html entities and so they weren't showing up. It was supposed to be Comparable<BlogPost>

Also, I should have noted that the compareTo() method used to compare the Date objects is not the compareTo() method being written, i.e., this is not a recursive method.