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 Comparable

isaac schwartzman
isaac schwartzman
963 Points

how exactly do I compare because I don't seem to understand

I've seen the associated video 4 times and for some reason I just don't grasp this concept and I'm to stubborn to skip it.

package com.example;

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 int compareTo(BlogPost post){
   BlogPost post =new BlogPost();
     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;


1 Answer

Matthew Caloger
Matthew Caloger
12,903 Points

Honestly, this example is very confusing to illustrate the idea.

First, the answer to the question:

// make sure the class implements 'Comparable'  so that Compare.compareTo() can be overridden
public class BlogPost implements Comparable<BlogPost>

  public int compareTo(BlogPost blogPost){
    if (this.equals(blogPost)) {
      return 0;

    int compareDate = mCreationDate.compareTo(blogPost.mCreationDate);
    return compareDate;

Here's the official doc page for Comparable interface, it takes an object and only contains an int method compareTo() that operates on the given object. The idea of Comparable is to compare the current object to another object of the same type, and return 0 if they're the same, -1 if the other object would come before the current object, and 1 if the current object would come before the other object. Note that the idea is to come up with your own sort between two objects of the same type, such as comparing number or strings (where "A" would come before "B", as an example).

This lesson's example is confusing because you're using Date.compareTo(), which exists in the Date library.

Tutorialpoint has a good example of using Date.compareTo()

StackOVerflow discussion on implementing Comparable

Good luck!