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 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,365 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