1
00:00:00,150 --> 00:00:03,570
You can sort objects that implement the comparable interface.

2
00:00:06,920 --> 00:00:10,550
The sorts of method can easily sort comparable objects.

3
00:00:13,400 --> 00:00:16,370
In this method, you will implement the comparable interface.

4
00:00:18,640 --> 00:00:21,820
The sought method can comparable objects.

5
00:00:24,450 --> 00:00:27,660
No, comparable is an interface that classes can implement.

6
00:00:30,380 --> 00:00:33,470
The comparable interface defines one method compared to.

7
00:00:37,870 --> 00:00:43,150
It also expects a generic, just like a function receives a parameter, a class or an interface can

8
00:00:43,150 --> 00:00:44,200
receive a generic.

9
00:00:47,400 --> 00:00:49,770
And the generic fildes, the method signature.

10
00:00:51,100 --> 00:00:53,620
So if you implement the comparable interface like so.

11
00:00:57,950 --> 00:00:59,270
Comparable pense.

12
00:01:02,100 --> 00:01:05,370
You'll have to override a method that's compared to Pentz.

13
00:01:08,090 --> 00:01:13,250
If you implement the compatible interface, like so comparable shirt, you'll have to override a method

14
00:01:13,260 --> 00:01:14,660
that's compared to a shirt.

15
00:01:15,120 --> 00:01:20,820
Now don't overthink what a generic is, just like a function can receive parameters and interface or

16
00:01:20,820 --> 00:01:22,340
a class can receive a generic.

17
00:01:24,780 --> 00:01:28,920
If you look back at the requirements, products are required to be sortable by price.

18
00:01:33,010 --> 00:01:37,270
We'll start with pens, the pens class will implement the comparable interface.

19
00:01:40,210 --> 00:01:43,720
And using a generic, you have to pass in the pens type.

20
00:01:45,390 --> 00:01:50,030
And right here, we're signing a contract that we're going to override this method inside the interface,

21
00:01:50,340 --> 00:01:51,660
the method is compared to.

22
00:01:59,570 --> 00:02:01,530
And right now, we've honored the contract.

23
00:02:02,210 --> 00:02:04,190
Let's play around with the generic a little.

24
00:02:06,010 --> 00:02:07,660
If I had object here.

25
00:02:12,870 --> 00:02:16,320
Then I would have to override a method that's compared to object.

26
00:02:19,300 --> 00:02:22,000
As you can see, the code wouldn't compile otherwise.

27
00:02:27,990 --> 00:02:30,330
But put pants back, that was just for example.

28
00:02:37,360 --> 00:02:43,030
And what I want to do is call this specified object in order to stay consistent with the documentation,

29
00:02:43,270 --> 00:02:44,200
you'll see why some.

30
00:02:47,130 --> 00:02:50,100
OK, so compared to receives a specified object.

31
00:02:52,370 --> 00:02:55,430
And let's imagine there's a current object that's calling this method.

32
00:03:01,490 --> 00:03:04,040
We need to compare the price of the current object.

33
00:03:05,680 --> 00:03:11,020
Against the price of the specified object, the one that's being passed in, ultimately, we're going

34
00:03:11,020 --> 00:03:14,830
to use that comparison to determine how the objects are going to be sorted.

35
00:03:18,920 --> 00:03:22,430
But it uses an integer to compare the two objects, but how?

36
00:03:23,680 --> 00:03:27,760
Let's look at the documentation, I linked it in the resources, if you want to open it and follow along

37
00:03:27,760 --> 00:03:29,950
with me, take some time to do so now.

38
00:03:30,950 --> 00:03:37,610
Anyways, scroll down to the returns line and the documentation tells us how Java interprets the result

39
00:03:37,610 --> 00:03:38,480
from compare to.

40
00:03:40,210 --> 00:03:45,640
If it sees that the energy that gets returned is negative, then it assumes the current object is less

41
00:03:45,640 --> 00:03:49,820
than the specified object if it sees that the return value is zero.

42
00:03:50,140 --> 00:03:53,920
It assumes the current object and the specified object are equal.

43
00:03:55,250 --> 00:04:00,890
And if it sees that the return value is positive, then it assumes the current object is bigger than

44
00:04:00,890 --> 00:04:02,120
the specified object.

45
00:04:04,830 --> 00:04:05,340
That's good.

46
00:04:05,370 --> 00:04:08,220
So we're going to do is minus Superdog get price?

47
00:04:12,660 --> 00:04:14,970
And the specified object get price.

48
00:04:17,070 --> 00:04:18,000
And we get an error.

49
00:04:19,010 --> 00:04:23,900
And so you might think to write brackets end in order to typecast the result, but be careful.

50
00:04:28,140 --> 00:04:34,800
What if this is 99 cents more expensive than the specified object, in that case, typecasting to end

51
00:04:34,800 --> 00:04:37,140
will cut off the decimal and return zero.

52
00:04:39,060 --> 00:04:44,460
Which, according to the documentation, would indicate that these two objects are equal, which wouldn't

53
00:04:44,460 --> 00:04:45,090
be accurate.

54
00:04:46,920 --> 00:04:52,890
So what are you going to do is use math around and that's going to round to the nearest whole number

55
00:04:53,160 --> 00:04:57,900
and then we can typecast to enter because math got round returns, a long type.

56
00:04:58,990 --> 00:05:02,470
OK, before we start sorting, let's make sure this works back in Maine.

57
00:05:03,850 --> 00:05:08,380
Create another pant's object is equal to a new pant's.

58
00:05:11,140 --> 00:05:15,970
Waist 34, one two four point nine nine red, and we'll call it Jangala.

59
00:05:20,840 --> 00:05:25,580
OK, I'll compare this object against the first one pence three, but compared to.

60
00:05:28,010 --> 00:05:28,730
Pentz.

61
00:05:36,390 --> 00:05:42,090
And we get back a positive number, which indicates Pince three has a higher price than the specified

62
00:05:42,090 --> 00:05:42,690
object.

63
00:05:46,080 --> 00:05:47,220
If we reverse it.

64
00:05:54,850 --> 00:06:00,070
We get back a negative, no, pence has a lower price than the specified object.

65
00:06:02,610 --> 00:06:04,590
And if you compare pens and pens to.

66
00:06:08,330 --> 00:06:13,760
We'll have to make this a pense object, because Penns can be of type discounted oil, but something

67
00:06:13,760 --> 00:06:16,520
that is discounted will won't necessarily be pentz.

68
00:06:20,220 --> 00:06:25,650
And here we get back zero, which means the current object calling this method has a price equal to

69
00:06:25,650 --> 00:06:26,910
the specified object.

70
00:06:29,320 --> 00:06:34,360
All right, our pants class effectively implements the comparable interface, which means now we can

71
00:06:34,360 --> 00:06:40,840
use the sorta method sort can you use compare to to determine how to sort comparable objects?

72
00:08:45,420 --> 00:08:47,550
What I want you to do is remove everything from main.

73
00:08:51,530 --> 00:08:54,890
And copy the code from the resources called sample one.

74
00:09:04,790 --> 00:09:08,290
OK, now we can sort this array using a raised sort.

75
00:09:13,950 --> 00:09:19,140
SORT is going to call the compare to method from one object against another object, and it's going

76
00:09:19,140 --> 00:09:23,070
to keep doing that until the objects are sorted from lowest the highest price.

77
00:09:23,340 --> 00:09:24,180
So let's try it out.

78
00:09:55,300 --> 00:10:01,090
So the sought method calls compared to from the second object and passes the first one as the specified

79
00:10:01,090 --> 00:10:01,600
object.

80
00:10:04,730 --> 00:10:06,950
Compared to is going to return a positive number.

81
00:10:09,140 --> 00:10:13,430
So Saut is going to know that this object is higher than this one.

82
00:10:14,650 --> 00:10:20,890
So Saut is going to know to place this object after this one because it's higher and if I just fast

83
00:10:20,890 --> 00:10:26,590
forward, it effectively compares every object in the array and depending on whether the results return

84
00:10:26,590 --> 00:10:32,260
to us, positive or negative, it knows to place the smaller objects in the beginning and the larger

85
00:10:32,260 --> 00:10:33,340
objects in the end.

86
00:10:41,470 --> 00:10:42,340
And beautiful.

87
00:10:50,510 --> 00:10:52,660
We can make the shirt class comparable as well.

88
00:10:55,000 --> 00:10:57,670
The short class is going to implement the comparable interface.

89
00:11:13,110 --> 00:11:14,550
We'll do the same thing as before.

90
00:11:28,100 --> 00:11:29,870
Wait, did I just copy and paste code?

91
00:11:30,290 --> 00:11:31,730
Yes, I did, and that's bad.

92
00:11:32,210 --> 00:11:36,670
If you ever find yourself copy and paste and code, it means you're doing something wrong.

93
00:11:37,010 --> 00:11:41,860
In this case, the shirt and pants class have the exact same implementation for compared to.

94
00:11:42,260 --> 00:11:46,010
So what if instead we define that implementation inside the parent.

95
00:11:46,460 --> 00:11:49,970
That way both shirt and pens can inherit the same method.

96
00:11:52,790 --> 00:11:54,560
So remove everything here.

97
00:12:05,600 --> 00:12:06,380
And here.

98
00:12:16,420 --> 00:12:21,670
We can make the product cost comparable, which is automatically going to make its children classes

99
00:12:21,670 --> 00:12:22,710
comparable as well.

100
00:12:38,810 --> 00:12:41,210
And I'm going to return this stock get price.

101
00:12:45,850 --> 00:12:48,400
Minus the price of the specified object.

102
00:12:59,630 --> 00:13:01,460
We're going to have to round this to a whole number.

103
00:13:08,580 --> 00:13:09,840
And typecast isn't.

104
00:13:16,360 --> 00:13:20,530
Now, both shirt and pants are going to inherit this method from their parents.

105
00:13:26,430 --> 00:13:30,570
Remove all the code in Maine and get the second code sample code sample to.

106
00:13:37,490 --> 00:13:39,860
OK, and a few breakpoints around the debugger.

107
00:13:57,160 --> 00:14:00,190
Pens can take the form of product because of polymorphism.

108
00:14:02,510 --> 00:14:07,760
Current object has a lower price than the specified object, so a negative value is going to be returned.

109
00:14:11,560 --> 00:14:17,590
And when it sees a negative value, it's going to know, OK, the current object is smaller than the

110
00:14:17,590 --> 00:14:20,590
specified object, so it needs to come before.

111
00:14:21,790 --> 00:14:27,970
And it basically keeps doing that across the entire array until it sorts the objects from lowest priced,

112
00:14:27,970 --> 00:14:28,920
the highest price.

113
00:14:29,440 --> 00:14:30,470
And one more thing.

114
00:14:30,640 --> 00:14:34,540
Notice how it's comparing a shirt object against the pants object.

115
00:14:34,870 --> 00:14:40,570
But as a product, that's polymorphism, which makes our code more flexible, which is pretty amazing.

116
00:14:41,320 --> 00:14:42,450
Let's fast forward here.

117
00:14:42,490 --> 00:14:43,750
We're not going to go through the whole thing.

118
00:14:44,540 --> 00:14:49,990
And as you can see, it sorts every product shirt or pants from lowest priced, the highest price.

119
00:14:51,140 --> 00:14:54,320
The sort of method can easily sort comparable objects.

120
00:14:56,300 --> 00:15:00,320
Sort can use compare to to determine how to sort your objects.

