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

Java - Sorting creationDate(s) cronologcally? (3/3)

Honestly, I don't get what I'm supposed to do in this coding challenge. Do I have to create a Date[] array and use sort(dates, Collections.reverseOrder())? And where (in which method) to put all this? The instruction is a little unclear to me. Appreciate constructive help.

com/example/BlogPost.java
package com.example;

import java.util.Date;
import java.util.Arrays;
import java.util.Collections;

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

  @Override
  public int compareTo(Object obj) {
    BlogPost post = (BlogPost)obj;

    if(equals(post)) {
      return 0; 
    }
    return 1;

  }
}
Luca Cunico
Luca Cunico
Courses Plus Student 14,196 Points

Hi, You're not supposed to create any List or Array and sort them. What the challenge is asking: Update the compareTo method so the older post are positioned before the newest.

Let me explain better: in this case, if you have two posts with the same title, author, body and category you're not sure that the older is placed before.

I might be wrong but this is how I did it:

@Override
  public int compareTo(Object obj) {
    BlogPost post = (BlogPost)obj;

    if(this.equals(post)) {
      return 0; 
    }
    return this.mCreationDate.compareTo(post.mCreationDate);

  }

P.S. English is not my mother tongue, please excuse any errors on my part.