You can sort objects that implement the comparable interface.
The sorts of method can easily sort comparable objects.
In this method, you will implement the comparable interface.
The sought method can comparable objects.
No, comparable is an interface that classes can implement.
The comparable interface defines one method compared to.
It also expects a generic, just like a function receives a parameter, a class or an interface can
receive a generic.
And the generic fildes, the method signature.
So if you implement the comparable interface like so.
Comparable pense.
You'll have to override a method that's compared to Pentz.
If you implement the compatible interface, like so comparable shirt, you'll have to override a method
that's compared to a shirt.
Now don't overthink what a generic is, just like a function can receive parameters and interface or
a class can receive a generic.
If you look back at the requirements, products are required to be sortable by price.
We'll start with pens, the pens class will implement the comparable interface.
And using a generic, you have to pass in the pens type.
And right here, we're signing a contract that we're going to override this method inside the interface,
the method is compared to.
And right now, we've honored the contract.
Let's play around with the generic a little.
If I had object here.
Then I would have to override a method that's compared to object.
As you can see, the code wouldn't compile otherwise.
But put pants back, that was just for example.
And what I want to do is call this specified object in order to stay consistent with the documentation,
you'll see why some.
OK, so compared to receives a specified object.
And let's imagine there's a current object that's calling this method.
We need to compare the price of the current object.
Against the price of the specified object, the one that's being passed in, ultimately, we're going
to use that comparison to determine how the objects are going to be sorted.
But it uses an integer to compare the two objects, but how?
Let's look at the documentation, I linked it in the resources, if you want to open it and follow along
with me, take some time to do so now.
Anyways, scroll down to the returns line and the documentation tells us how Java interprets the result
from compare to.
If it sees that the energy that gets returned is negative, then it assumes the current object is less
than the specified object if it sees that the return value is zero.
It assumes the current object and the specified object are equal.
And if it sees that the return value is positive, then it assumes the current object is bigger than
the specified object.
That's good.
So we're going to do is minus Superdog get price?
And the specified object get price.
And we get an error.
And so you might think to write brackets end in order to typecast the result, but be careful.
What if this is 99 cents more expensive than the specified object, in that case, typecasting to end
will cut off the decimal and return zero.
Which, according to the documentation, would indicate that these two objects are equal, which wouldn't
be accurate.
So what are you going to do is use math around and that's going to round to the nearest whole number
and then we can typecast to enter because math got round returns, a long type.
OK, before we start sorting, let's make sure this works back in Maine.
Create another pant's object is equal to a new pant's.
Waist 34, one two four point nine nine red, and we'll call it Jangala.
OK, I'll compare this object against the first one pence three, but compared to.
Pentz.
And we get back a positive number, which indicates Pince three has a higher price than the specified
object.
If we reverse it.
We get back a negative, no, pence has a lower price than the specified object.
And if you compare pens and pens to.
We'll have to make this a pense object, because Penns can be of type discounted oil, but something
that is discounted will won't necessarily be pentz.
And here we get back zero, which means the current object calling this method has a price equal to
the specified object.
All right, our pants class effectively implements the comparable interface, which means now we can
use the sorta method sort can you use compare to to determine how to sort comparable objects?
What I want you to do is remove everything from main.
And copy the code from the resources called sample one.
OK, now we can sort this array using a raised sort.
SORT is going to call the compare to method from one object against another object, and it's going
to keep doing that until the objects are sorted from lowest the highest price.
So let's try it out.
So the sought method calls compared to from the second object and passes the first one as the specified
object.
Compared to is going to return a positive number.
So Saut is going to know that this object is higher than this one.
So Saut is going to know to place this object after this one because it's higher and if I just fast
forward, it effectively compares every object in the array and depending on whether the results return
to us, positive or negative, it knows to place the smaller objects in the beginning and the larger
objects in the end.
And beautiful.
We can make the shirt class comparable as well.
The short class is going to implement the comparable interface.
We'll do the same thing as before.
Wait, did I just copy and paste code?
Yes, I did, and that's bad.
If you ever find yourself copy and paste and code, it means you're doing something wrong.
In this case, the shirt and pants class have the exact same implementation for compared to.
So what if instead we define that implementation inside the parent.
That way both shirt and pens can inherit the same method.
So remove everything here.
And here.
We can make the product cost comparable, which is automatically going to make its children classes
comparable as well.
And I'm going to return this stock get price.
Minus the price of the specified object.
We're going to have to round this to a whole number.
And typecast isn't.
Now, both shirt and pants are going to inherit this method from their parents.
Remove all the code in Maine and get the second code sample code sample to.
OK, and a few breakpoints around the debugger.
Pens can take the form of product because of polymorphism.
Current object has a lower price than the specified object, so a negative value is going to be returned.
And when it sees a negative value, it's going to know, OK, the current object is smaller than the
specified object, so it needs to come before.
And it basically keeps doing that across the entire array until it sorts the objects from lowest priced,
the highest price.
And one more thing.
Notice how it's comparing a shirt object against the pants object.
But as a product, that's polymorphism, which makes our code more flexible, which is pretty amazing.
Let's fast forward here.
We're not going to go through the whole thing.
And as you can see, it sorts every product shirt or pants from lowest priced, the highest price.
The sort of method can easily sort comparable objects.
Sort can use compare to to determine how to sort your objects.