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 Interfaces

Kevin Faust
Kevin Faust
15,353 Points

[solved]im not sure what we were trying to accomplish in this video and i got many questions..

Hi, i have some questions

In the Treet.java file:

1) if we only want to take in treets, why not just do "public int compareTo(Treet obj){}"? then we dont have to waste a line downcasting.

2) what does string.format() do? does it have something to do with us putting at object inside it?

In the Example.java file:

1) i dont understand how this works: Arrays.sort(treets);

are we sorting the toString() of each object in alphabetical order? but then craig said that they were sorted by their date and time?? what exactly are we sorting?

Interfaces:

1) i still dont understand interfaces. where did interface come into play and what was the point of implements Comparable? is this something we need to do to use the compareTo() method? because their names sound similar and its the only reason i could think of..

2) and compareTo()???? we spent a good chunk of time on this and im pretty sure we didnt even use this but rather did something else in the end. are we going to use this in the next video? what will this compareTo method even be doing

i dont even remember what we're trying to accomplish...can someone explain what our end goal is?

i need a explanation of all this stuff because this video just got me more lost than before...

thank you

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

ok neeevermind. i was reading craig's explanation here: https://teamtreehouse.com/community/how-do-i-do-this-15 and i could start connecting the dots

Craig Dennis
Craig Dennis
Treehouse Teacher

Happy to give these an answer Kevin if you still want it. Anything still confusing?

Kevin Faust
Kevin Faust
15,353 Points

Hey Craig Dennis,

Sorry for giving you such a hassle. I'm usually not this annoying.

Can you give me some more information on how sorting works? I understand what we're trying to do but I still can't fully understand the code of it:

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

I understand how we first check if they're equal or not. If it returns 0 then we are supposed to leave the program. Is that generally what returning 0 does? If they are different then we check their dates. And if they are equal then we check description and then return the values. I know that java has an algorithm to sort the arrays but do we really need to know that? Or should we only be concerned about using 0 to check if they are the same and leave the automatic sorting to Java.

that being said, when we write `Arrays.sort(treets)', where do we store our sorted array? Is it stored back in treets?

If we want to sort things in an array, we always have to give the class the array belong to in the Comparable interface and give it the compareTo() method?

and lastly why did we use String.format()?

Thanks

Craig Dennis
Craig Dennis
Treehouse Teacher

Totally not annoying man! Please don't feel that way!

So the compareTo method is expected to return -1 if it is less than, 0 if equal and 1 if greater than the passed in object.

I know that java has an algorithm to sort the arrays but do we really need to know that? Or should we only be concerned about using 0 to check if they are the same and leave the automatic sorting to Java.

Basically what we are doing is choosing which column(s) should be sorted, notice that we are using the compareTo on the other fields to do that. That is the "built in" way this sorting works. I wanted to make sure that if the dates were the same that we would then further sort by the description. You've probably seen this in applications. First order by this column, and then this one. Like sort by Last Name, and then First name. I was trying to show that you could do that in a complex manner here.

when we write `Arrays.sort(treets)', where do we store our sorted array? Is it stored back in treets?

Yes Arrays.sort(arr) sorts the array in place using the objects compareTo method, and yes if you don't implement Comparable, it cannot be guessed which field you want to sort by.

Strings are funny things in Java. Not sure if you've seen my workshop The Thing About Strings or not, but concatenation using the + sign gets you into trouble especially when used in loops. I tend to lean towards always using String formatting over concatenation. This will come up deeper in the courses. String.format is what printf is using under the covers, so I wanted to show off that you can/should probably use that for legibility sake.

That help clear some things up?