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

Can not figure this out at all. Please help.

Challenge Task 1 of 3 Comparable

com/example/BlogPost.java
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 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;
  }
}

2 Answers

Grigorij Schleifer
Grigorij Schleifer
10,365 Points

Hi djwyatt,

allow me to comment the challenge:

package com.example;

import java.util.Date;

// TASK 1
public class BlogPost implements Comparable {
  // implement the Comparable interface

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

  // TASK 2
  @Override
  public int compareTo(Object obj) {
// creating a method, that returns an integer and accepts an Object as parameter

    BlogPost post = (BlogPost) obj;
    // Here you cast the parameter "obj" into a BlogPost Object. 

    if(equals(post)) {
     // equals is referring to the BlogPost you are inside
     // and "post" is representing the Object that was casted to BlogPost above
     // so "equals(post)" says: if the object that is castet to BlogPost is equals to BlogPost you are inside
     // return 0
   return 0;
    }
   return 1;
// if they are not the same, return 1
  }

}

For the Task 3 you will replace the last return statement with the comparation prozess and return the result (0 or 1)

 mCreationDate.compareTo(post.mCreationDate);
// this line is ordering the creation dates chronologicaly 
// the oldest go first :)
// mCreationDate is a Date of the BlogPost you are inside
// post.mCreationDate gets you the creation date of the BlogPost that you are comparing with to mCreationDate
// if they are not equal, the returned result will be 1

The if statement is ensuring that the BlogPost is not compared to itself.

Does it make sense?

Grigorij

Yes it makes sense, and thank you for the help Grigorji!