Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Organizing Data Comparable

Tim Strand
Tim Strand
22,458 Points

i dont understand what challenge 3/3 is asking for. I have added a sort which doesnt work i keep getting compiler errors

a couple issues: 1) compiler errors on my Arrays.sort even though it matches what we used in class 2) not sure what its wanting when getting error Expected -1 but got 1

i had assumed this challenge had to do with sorts since it says lets order the blog posts by creation date.

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 other = (BlogPost) obj;
    if (equals(other)) {
     return 0; 
    }
    int dateCmp = mCreationDate.compareTo(other.mCreationDate);
    if (dateCmp ==0) {
     return mTitle.compareTo(other.mTitle); 
    }
    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;
  }

 BlogPost[] blogDates = new BlogPost[10];
  Arrays.sort(blogDates);



}

1 Answer

Grigorij Schleifer
Grigorij Schleifer
10,364 Points

Hi Tim,

  public int compareTo(Object obj) {
   BlogPost other = (BlogPost) obj;
    if (equals(other)) {
// if your BlogPost is equal to another BlogPost (accepted as parameter)
// return 0
     return 0; 
    }
   return mCreationDate.compareTo(other.mCreationDate);
// no need to create an extra integer or if loop
// mCreationDate is the basis of our comparison
// on this basis you call the compareTo method and give that method the creation date of the parameter obj 
// casted as BlogPost to be compared and sorted 
// Java automaticly sort the dates for you
  }

I am not sure, but if the creation date of your BlogPost is younger, then the returned value will be -1. In the opposite case > 0.

The returned integer helps the colĀ“mpiler to sort the creation dates chronologically ...

I hope that makes sense :)

Grigorij