Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:01,320 --> 00:00:02,969
We can also sort alphabetically.
2
00:00:05,130 --> 00:00:08,250
The goal of this lesson is to sort by class name and price.
3
00:00:10,120 --> 00:00:13,750
Open up the folder for this lesson by following this path inside the resources.
4
00:00:19,260 --> 00:00:21,720
The string class also implements comparable.
5
00:00:24,150 --> 00:00:27,420
It follows that the string class overrides compared to.
6
00:00:29,310 --> 00:00:35,130
The compared to method for string objects returns a negative number if the character in the first string
7
00:00:35,130 --> 00:00:38,880
has a lower Unicode value than the character in the specified object.
8
00:00:41,500 --> 00:00:44,440
Zero, if the strings are identical, which makes sense.
9
00:00:46,340 --> 00:00:51,830
And a positive number, if a character in the first string has a higher Unicode value than a character
10
00:00:51,830 --> 00:00:52,880
in the second string.
11
00:00:55,430 --> 00:01:00,350
We'll test the first case first, we have John and Amy, we'll call Amy compared to.
12
00:01:02,110 --> 00:01:02,650
John.
13
00:01:05,010 --> 00:01:09,150
The letter A. is expected to have a lower Unicode value than the letter J.
14
00:01:09,480 --> 00:01:11,340
So that's going to return a negative number.
15
00:01:12,780 --> 00:01:13,390
Perfect.
16
00:01:13,860 --> 00:01:15,630
Now, I'll compare Amy to Amy.
17
00:01:21,950 --> 00:01:23,870
Returns zero expected.
18
00:01:25,250 --> 00:01:27,440
Now, I'm going to compare John against Jane.
19
00:01:39,960 --> 00:01:47,550
JNJ, same Unicode value, but the letter O comes after a so we can expect a higher Unicode value perfect.
20
00:01:49,290 --> 00:01:51,570
And if we reverse it, we get a negative number.
21
00:01:56,060 --> 00:02:00,920
So based on this information that you just got, go back to the product application.
22
00:02:02,700 --> 00:02:08,789
And what I want you to do is sort the products by alphabetical order based on their class type, then
23
00:02:08,789 --> 00:02:10,320
sort the rest by price.
24
00:02:11,690 --> 00:02:17,270
Here are some hints you can get the class type by using get class and you can get the name of the class
25
00:02:17,270 --> 00:02:19,690
by using get class, get simple name.
26
00:02:20,450 --> 00:02:23,090
I'll give you the chance to pause the video and try this out.
27
00:02:27,910 --> 00:02:29,270
I hope you figured it out.
28
00:02:29,290 --> 00:02:33,850
So what I'm going to do is inside compared to I'll get the name of the current object.
29
00:02:35,090 --> 00:02:36,200
String class name.
30
00:02:39,030 --> 00:02:41,970
Is equal to this doGet class, that simple name.
31
00:02:43,190 --> 00:02:45,980
And I'll get the last name of the specified object.
32
00:02:58,880 --> 00:03:02,360
All right, now we're going to check if the class names aren't equal.
33
00:03:07,990 --> 00:03:12,820
If that's the case, we're going to return the result from comparing the first class name to the second
34
00:03:12,820 --> 00:03:13,090
one.
35
00:03:22,820 --> 00:03:28,820
Here we're comparing two strings, and if the two objects don't happen to share the same class, so,
36
00:03:28,820 --> 00:03:34,310
for example, if this is pants and this is shirt compared to is going to return an integer, that depends
37
00:03:34,310 --> 00:03:35,750
on the letters in each name.
38
00:03:38,220 --> 00:03:40,170
Let's test this out by running the debugger.
39
00:04:06,060 --> 00:04:08,790
These two objects don't share the same class.
40
00:04:09,060 --> 00:04:15,870
This is Pence, this is Schertz, the letter s is expected to have a higher Unicode value than the letter
41
00:04:15,870 --> 00:04:16,170
P.
42
00:04:16,589 --> 00:04:21,959
So this is going to return a positive value, which means shirt is going to come after pence.
43
00:04:41,600 --> 00:04:47,750
Now we're comparing pants and pants, the two pants are part of the same class, however, the price
44
00:04:47,750 --> 00:04:51,340
of the current object is higher than the price of the specified object.
45
00:04:51,650 --> 00:04:53,980
So compared to returns, a positive number.
46
00:04:54,140 --> 00:04:57,930
So it's going to sort the current object that come after the specified one.
47
00:04:58,250 --> 00:05:02,300
And so basically it's going to keep comparing each object against another object.
48
00:05:04,260 --> 00:05:06,960
First, it prioritizes alphabetical order.
49
00:05:08,800 --> 00:05:13,870
And if the two objects share the same class, then it's going to sort of by comparing the price of each
50
00:05:13,870 --> 00:05:14,470
product.
51
00:05:14,890 --> 00:05:20,050
Ultimately, we get an array where objects of different classes are sorted based on the class name,
52
00:05:20,410 --> 00:05:23,650
and every object of the same class are compared by price.
53
00:05:24,550 --> 00:05:31,000
The sort method can easily sort to comparable objects, sort uses compare to to determine how to sort
54
00:05:31,000 --> 00:05:31,720
your objects.
55
00:05:32,020 --> 00:05:37,660
And if the two objects don't have the same class, the one with characters that have a lower Unicode
56
00:05:37,660 --> 00:05:39,670
value are going to be sorted first.
57
00:05:40,030 --> 00:05:44,620
If the names happen to be equal, then the objects are compared based on their price.
58
00:05:45,010 --> 00:05:50,410
As a result, our comparable objects are sorted based on alphabetical order and price.
5741
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.