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

Sort objects

Hi think the question 3/3 of the Comparable subject is confusing, or I am missing something

Challenge Task 3 of 3

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

There is no Main class provided in the Challenge, and compareTo override, returns an integer; not a list of objects. So, How do I return a sorted list of objects without writing or adding a method to the BlogPost class ... but the instructions is not for adding a new method.

Can someone please explain this question ...

I got the CompareTo part I got the Arrays.sort part

I just need to know what the Challenge question is asking for ... when no main is provided to pass 2 objects for sorting so we can return a sorted list.

Thanks you

PS. This question is pretty confusing, I recommend re-writing

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 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){
    com.example.BlogPost blogPost = (com.example.BlogPost) obj;     
    if(this.equals(blogPost)) {
      return 0;
    }
    return 1;
  }    
}

2 Answers

I answered my own question by assuming that the "Question" is not asking for a sorted array, but for comparing if one object is older than the other object.

in this case, a trinary operator on the creation date will do ... something like this would work.

@Override public int compareTo(Object obj){ com.example.BlogPost blogPost = (com.example.BlogPost) obj;
if(this.equals(blogPost)) { return 0; } return (this.mCreationDate.after( blogPost.mCreationDate)) ? 1 : -1; }

Again, I recomment re-writing the question in the challenge.

Chris Jones
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Jones
Java Web Development Techdegree Graduate 23,933 Points

Thanks for the help, Mark! Just curious, but do you know why we couldn't write your last return statement above as:

? 1: 1

If the result of the last return statement is true, which produces a 1, then why are we casting it to -1? Shouldn't we just be able to cast it to 1? I tried this in the code challenge, but it didn't work.

Thanks for the help!

The purpose of testing a condition is to return one of 2 outcome ... true, false 1, 0 good, bad

  1. -1 red, green

it really does it matter, you are testing for a condition and and telling the code, base on the outcome of the test, please return a "status"

In the above example, I am testing the creation date of two blogs ... (this.mCreationDate.after( blogPost.mCreationDate)) ? 1 : -1; }

this .... is the blog I have in hand and blogPost ... is the blog I am comparing to "this"

if(this creation date is after blogPost creation date, I want the code to return 1, otherwise, I want it to return -1. The 1 and -1 are "the expected" return values by the caling routine ... which is expecting 1, 0 or -1.

If the calling routine was expecting , "Go, "nevermind", "noGo", I would have written the

blogPost.mCreationDate)) ? 1 : -1;

as

blogPost.mCreationDate)) ? "Go" : "noGo";

note: we are not casting, we are returning the result of a test to the calling routing.

Hope this helps