Java Java Data Structures Exploring the Java Collection Framework Upgrade Comparable to use Generics

michael lee
michael lee
5,176 Points

What am I doing wrong?

what am I missing?

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

import java.io.Serializable;
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(BlogPost other) {

    if (equals(other)) {
      return 0;
    }
    return mCreationDate.compareTo(other.mCreationDate);
  }

  public String[] getWords() {
    return mBody.split("[^\\w']+");
  }

  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;
  }
}

1 Answer

Hi. Your method is absolutely fine. You just forgot to have your class implement Comparable<BlogPost>.

public class BlogPost implements Comparable<BlogPost> {

Hope that helps :)