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

Tristan Smith
Tristan Smith
3,171 Points

Task 3 - Sorting the array

A bit confused. I understand the comparison, and I understand how it sorts. But I don't understand how to write the code that will sort the blogposts.

Are there arguments that I need to get? Is there something that will be passed in from say "Example.java" that I need to expect? Do I ensure that all BlogPost objects are sent to a blogpost array after creation, then sort from there? >_<

I've noticed that with these tasks I sometimes feel so derpy, the answer is right in front of me but I can't seem to wrap my head around it, or sometimes there's things that I need to do that aren't implied or mentioned, but I only get what the task wants after a "bummer" error. In this case, I don't even know where to begin. I tried to get my brain going by writing a sort method, but I don't know where to go from here.. :/

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

import java.util.Arrays;
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;
  }

  @Override
  public int compareTo(Object obj){
    BlogPost blogpost = (BlogPost) obj;
    if (this.equals(blogpost)){
      return 0;
    }
    return 1;
  }

  public BlogPost[] sortPosts(Object obj){
    return Arrays.sort(obj);
  }

  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

Kourosh Raeen
Kourosh Raeen
23,733 Points

If the object references refer to the same object then they are equal so you return 0, as you've already done. Otherwise, they are different blog posts and we are asked to do the comparison based on the blogs' creation dates. To do that we can simply call the Date class's compareTo() method on mCreationDate and pass to it the creation date for the other blog, which we can get using the getter method:

@Override
public int compareTo(Object obj) {
    BlogPost other = (BlogPost) obj;
    if (this.equals(other)) {
        return 0;
    } else {
          return mCreationDate.compareTo(other.getCreationDate());
    }
}
Tristan Smith
Tristan Smith
3,171 Points

This is the text from task 3, it does imply sorting. Am I incorrect? x_x

"Alright, now let's order the blog post's creation date chronologically, oldest first.

NOTE: The app where this class is being used ensures that no two blog posts are posted at the same time, so you do not need to worry about any edge cases."

edit: I appreciate your help by the way.

Kourosh Raeen
Kourosh Raeen
23,733 Points

You don't need to do the actual sorting for this challenge. We don't have a list of blogs to sort here. You just need to make the blogs sortable by implementing the Comparable interface and writing the compareTo() method.

Tristan Smith
Tristan Smith
3,171 Points

Thank you for your help, I was so focused on trying to figure how to sort it that I didn't finish the compareTo method.