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

chris munley
chris munley
2,204 Points

Im really not sure how to sort this blogpost

Im not sure if im on the right path to getting this right or not I have been stuck for a little while on this. I know the code isnt complete I just want to know if im sorting right at all or if there is something else I should be doing. Thanks!

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 int compareTo(Object obj) {
    BlogPost other = (BlogPost) obj;

    if (equals(other)) {
    return 0 ; } else {
    return 1; }
  }

  public void sort(Date[] array) {
    for(int i = 0; i < (array.length - 1); i++) {
      if(Date[i] > Date [i+1]) {
        Date temp = array[i];
        Date temp2 = array[i+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;
  }
}
chris munley
chris munley
2,204 Points

I am supposed to sort by creation date chronologically I think

2 Answers

Sergey Podgornyy
Sergey Podgornyy
20,660 Points

Try this code:

public int compareTo(Object obj) {
    // Task 2 of 3
    // In this step, cast the Object passed to the compareTo method
    // to a com.example.BlogPost. Check for equality first, if they
    // are equal return 0. Leave the default return of 1 in place
    // for this step.
    BlogPost blogpost = (BlogPost) obj;
    if (equals(blogpost)) {
      return 0;
    }
    // Task 3 of 3
    // Alright, now let's order the blog post's creation date
    // chronologically, oldest first.
    // NOTE: The app where this class is being used ensures that no
    // two blog posts are posted at the same time, so you do not need
    // to worry about any edge cases.
    return mCreationDate.compareTo(blogpost.mCreationDate);
  }
Haley Bengtson
seal-mask
.a{fill-rule:evenodd;}techdegree
Haley Bengtson
Front End Web Development Techdegree Student 14,334 Points

I was stuck on this problem as well. I got mine to work with this code, but I am having trouble understanding how this sorts the blog posts by their dates. I thought that this only compared the two, without sorting and returned (-1, 0, 1).

Hello, Look at this two methods should it be correct by changing some places???

@Override public int compareTo(Object obj){ Treet other = (Treet) obj; if (equals(other)){ return 0; } int dateCmp = mCreationDate.compareTo(other.mCreationDate);

  if(dateCmp == 0){
    return mDescription.compareTo(other.mDescription); 
  }
return dateCmp;

}

@Override public int compareTo(Object obj){ Treet other = (Treet) obj; if (equals(obj)){ return 0; } int dateCmp = mCreationDate.compareTo(obj.mCreationDate);

  if(dateCmp == 0){
    return mDescription.compareTo(obj.mDescription); 
  }
return dateCmp;

}