Java Java Data Structures Organizing Data Comparable

Jun Dong
Jun Dong
2,554 Points

Practice Lesson Error

It's giving me a error saying make sure that your class implements the comparable method, how do I fix my code?

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

import java.util.Date;

public class BlogPost {
  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 int compareTo(Object obj) {
    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;
  }
}

2 Answers

Alexander Besse
PRO
Alexander Besse
Full Stack JavaScript Techdegree Graduate 30,531 Points

Hi Jun Dong!

You'll need to add implements Comparable to the end of your public class BlogPost to make sure that your class implements the Comparable Object.

Other than that, your compareTo method looks great! Good work.

Happy Coding.