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

Udayakumar T
Udayakumar T
11,544 Points

Hi, Please help me...

HI,

I have checked and returned the creation date in compare to method,but still task 1 is no longer is passing.

Please help me....

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){
  BlogPost ob = (BlogPost) obj;
  if(this.equals(ob)){
    return 0; 
  }
  int cmp = this.mCreationDate.compareTo(mCreationDate);
  if(cmp==0){
    return this.mCreationDate.compareTo(mCreationDate);
  }
  return 1; 
}

}

1 Answer

Hello

I think you have a minor type in your class. This is my version.

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

and I created this simple main class for you test test:


import java.time.Instant;
import java.util.Date;

public class Main {

    public static void main(String[] args) {
        Instant instant1 = Instant.now().minusSeconds(36000);
        Instant instant2 = Instant.now().plusSeconds(36000);
        BlogPost bp1 = new BlogPost("me", "mine", "My Post", "Funny", Date.from(instant1));
        BlogPost bp2 = new BlogPost("me", "mine", "My Post", "Funny", Date.from(instant2));
        BlogPost bp3 = new BlogPost("me", "mine", "My Post", "Funny", Date.from(instant1));
        //
        //test 1
        if(bp1.compareTo(bp2) ==0){
            System.out.println("Yep, bp1 and bp2 are equal");
        }else{
            System.out.println("Nope, bp1 and bp2 are not equal");

        }
        //
        //test 2
        if(bp1.compareTo(bp3) ==0){
            System.out.println("Yep, bp1 and bp3 are equal");
        }else{
            System.out.println("Nope, bp1 and bp3 are not equal");

        }
    }
}

this should return the following from your class compareTo:

Nope, bp1 and bp2 are not equal
Yep, bp1 and bp3 are equal