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

"Alright, now let's order the blog post's creation date chronologically, oldest first." What am I supposed to do?

This is task 3 of the challenge regarding Java Data Structures > Organizing Data > Comparable > Task 3/3. I literally have no idea what this task in the exercise is asking me to do. Previous tasks of the exercise had me implementing a compareTo() method on a BlogPost class.

The question is: "Alright, now let's order the blog post's creation date chronologically, oldest first." First off, you can't "order" a field. I take it the task means that I should be ordering BlogPost objects.

But let's order them where? In the compareTo() method I've been working on? That makes zero sense, since this method is just supposed to return an int, indicating that the two objects are equal or not equal. And there is no list or array to order things in.

Is this supposed to mean that I'm supposed to be writing some new order() method or something?

Seriously, someone needs to reword this question to explain what in the foobar they want you to do. Totally unclear.

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 int compareTo(Object obj){
    if(obj instanceof BlogPost)
    {
      int code = (BlogPost)obj == this ? 0  : 1;
      return code;
    }
    return 1;
  }

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

1 Answer

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

You are right, it could use a few more hints in the right direction in the task 3/3. The instructors are trying to give you scenarios where you have to make trial-error attempts at solving it because this is by far the best way to learn in programming. Even the most experienced programmers make the same mistakes because sometimes the logic happening in your thought process doesn't exactly pan out to what the computer is doing.

I dont want to just give you the answer here, but I will tell you. YES you will be writing this logic in the compareTo method. Go back to the Interfaces video and skip to the 10:00 mark. This is the start of where he breaks down the entire compareTo method and what is happening. If you still are lost after watching this again, post which parts you aren't understanding. I will see if I can help.

compareTo is being used not just to compare an Object. You can compare whole objects or parts of the Object like class variables inside an object, such as mCreationDate or return types of methods or results expecting from any of the previous items I stated. :)