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

chris munley
chris munley
2,204 Points

How do i order the blogposts date chronologically i dont understand?

Sorry if its a dumb question but I really dont understand what I have to do.

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) {
    BlogPost other = (BlogPost) obj;

    if (equals(other)) {
    return 0 ; } else {
    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;
  }
}
J.D. Sandifer
J.D. Sandifer
18,813 Points

See my answer below, but know that it took me a while to work around a weird error and find a working solution.

The code checker wants return 0 to be the default case and for the method to only return a positive or negative integer if you've compared the dates. It's good practice to do it that way, but the error message "...Task 1 isn't passing..." isn't super helpful in figuring out what it wants you to do.

1 Answer

J.D. Sandifer
J.D. Sandifer
18,813 Points

The only requirement for sorting like this is to have a properly working compareTo() method. Here's a link to the Java documentation for the Comparable interface and the compareTo() method: Comparable Interface

The important sentences describing the method are these: "Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object."

That means the compareTo() method should return a negative integer (like -1) if the parameter blog post's date is older than this one, a positive integer (say 1) if the parameter blog post's date is newer, and a 0 otherwise.

You can compare Date objects like this: date1.before(date2) returns true if date1 comes before date2 and date1.after(date2) works for the reverse comparison. Java documentation: Date Object Comparison Methods