1 00:00:00,340 --> 00:00:05,430 In addition to copying, the arrays utility class also lets you sort an array. 2 00:00:05,430 --> 00:00:08,640 Now this is super handy in situations where you need a specific 3 00:00:08,640 --> 00:00:11,290 order to the elements in your array. 4 00:00:11,290 --> 00:00:15,550 In contrast to adding elements, let's see if I can renew your love for arrays by 5 00:00:15,550 --> 00:00:19,789 showing off how easy they are to arrange in the way that you want them to be. 6 00:00:20,980 --> 00:00:25,520 All right, so a couple things, I'm gonna set up scrratch.java here a little better. 7 00:00:25,520 --> 00:00:29,810 So first I'm gonna do is gonna add myself to this array here at the end, 8 00:00:29,810 --> 00:00:32,160 so I'm always there with my friends. 9 00:00:33,190 --> 00:00:36,630 And since it's getting a little bit longer, I'm gonna do this typical thing 10 00:00:36,630 --> 00:00:41,090 that you do with arrays, is you put each item on its own line. 11 00:00:41,090 --> 00:00:45,151 And because the curly braces are there, it just worked. 12 00:00:47,334 --> 00:00:51,180 Let's go ahead, let's get jshell started too while we're doing this editing. 13 00:00:51,180 --> 00:00:55,430 And the other thing I wanna do to show off sorting properly is I'm gonna add 14 00:00:55,430 --> 00:00:56,120 treasure here. 15 00:00:56,120 --> 00:00:59,430 We'll add treasure to the list. 16 00:00:59,430 --> 00:01:01,070 She's our new JavaScript teacher. 17 00:01:02,810 --> 00:01:07,560 Okay, and we know that we're gonna be using arrays static utility method. 18 00:01:07,560 --> 00:01:09,120 So let's import it. 19 00:01:09,120 --> 00:01:15,243 So in the scratch.java, we'll go ahead and 20 00:01:15,243 --> 00:01:19,926 say import java.util.Arrays. 21 00:01:19,926 --> 00:01:23,928 Okay, I've saved that file, that's looking good, and 22 00:01:23,928 --> 00:01:27,206 I'm going to go ahead and open scratch.java. 23 00:01:30,411 --> 00:01:34,759 And if we take a look at the current ordering of our friends array, 24 00:01:34,759 --> 00:01:38,670 we will see that they are out of alphabetical order. 25 00:01:38,670 --> 00:01:41,160 Awesome, so let's sort them. 26 00:01:41,160 --> 00:01:44,720 So we have our arrays class and, not surprisingly, 27 00:01:44,720 --> 00:01:48,660 on that arrays class there is a method called sort. 28 00:01:48,660 --> 00:01:51,364 And that sort takes an array and let's see what happens. 29 00:01:53,512 --> 00:01:59,680 And if we take a look friends now, we'll see that they're in alphabetical order. 30 00:01:59,680 --> 00:02:02,550 It doesn't get much more clear than that, does it? 31 00:02:02,550 --> 00:02:03,850 I do want you to notice though, 32 00:02:03,850 --> 00:02:08,500 that when we called this, it didn't return a copy of the array. 33 00:02:08,500 --> 00:02:12,140 It actually modified the array in place. 34 00:02:12,140 --> 00:02:13,900 So that is something to be aware of. 35 00:02:13,900 --> 00:02:19,400 Sorting does not return a copy, it actually modifies the original array. 36 00:02:19,400 --> 00:02:24,110 Wait a second, though, how do you think it knew to compare them alphabetically? 37 00:02:24,110 --> 00:02:29,040 Well, you might not know this, but all strings have a compareto method. 38 00:02:29,040 --> 00:02:30,330 Here, let's take a look really quick. 39 00:02:30,330 --> 00:02:31,955 I'm gonna press Ctrl + L to get back up here. 40 00:02:33,165 --> 00:02:38,245 So if I have the string Apple, I can call a method on it called compareTo, 41 00:02:38,245 --> 00:02:41,175 and compare it to another string. 42 00:02:41,175 --> 00:02:42,025 So let's take a look. 43 00:02:42,025 --> 00:02:44,555 So what happens if I compare apples to bananas? 44 00:02:46,385 --> 00:02:48,305 I get -1. 45 00:02:48,305 --> 00:02:52,708 So the way that this works is if the item on the left is less 46 00:02:52,708 --> 00:02:57,800 than the item on the right, it returns a negative number. 47 00:02:57,800 --> 00:03:01,010 Now, if it's equal it returns zero. 48 00:03:01,010 --> 00:03:04,726 Like so, so if Apple compareTo Apple, 49 00:03:04,726 --> 00:03:09,016 I'm not comparing apples and oranges here. 50 00:03:11,451 --> 00:03:12,890 We get 0, right? 51 00:03:12,890 --> 00:03:14,200 So, when it's equal. 52 00:03:14,200 --> 00:03:17,169 But if I go the other way, 53 00:03:17,169 --> 00:03:21,985 if I compare banana, compareTo Apple, 54 00:03:25,680 --> 00:03:31,050 You'll see it returns a positive number, meaning banana is greater that apple. 55 00:03:31,050 --> 00:03:35,970 So our arrays sort method uses this method to compare 56 00:03:35,970 --> 00:03:39,980 each element in our array until it's in the correct order. 57 00:03:41,030 --> 00:03:45,110 In fact, you can sort an array of any object as long as that 58 00:03:45,110 --> 00:03:48,200 object marks itself as being comparable and 59 00:03:48,200 --> 00:03:52,072 provides a method called comparedTo that returns these same values. 60 00:03:52,072 --> 00:03:57,740 This -1 for less than, 0 for equals, and a positive value for greater. 61 00:03:57,740 --> 00:04:00,900 That is out of the scope of this course, but we will get there shortly. 62 00:04:00,900 --> 00:04:04,240 Check the teacher's notes for more on interfaces and comparable. 63 00:04:04,240 --> 00:04:06,330 For now, though, a consolation prize. 64 00:04:06,330 --> 00:04:08,380 Let me show you off something pretty powerful. 65 00:04:08,380 --> 00:04:12,003 So let's say that we didn't wanna sort our list alphabetically, 66 00:04:12,003 --> 00:04:16,543 let's say we wanted to order our friends by how many characters were in their name. 67 00:04:16,543 --> 00:04:21,749 By default, the sort method uses the compareTo method of objects, but 68 00:04:21,749 --> 00:04:26,910 the sort method takes a second parameter which is of type comparator. 69 00:04:26,910 --> 00:04:29,950 So let's create one for our name length. 70 00:04:29,950 --> 00:04:34,530 So first thing to do is to import the comparator utility class. 71 00:04:34,530 --> 00:04:35,986 I'll go ahead and do it up here. 72 00:04:41,661 --> 00:04:42,720 Comparator. 73 00:04:44,270 --> 00:04:47,540 And then in jshell if you go and reopen a file. 74 00:04:47,540 --> 00:04:50,780 Let's go ahead and reopen scratch.java. 75 00:04:57,981 --> 00:05:01,630 And I'm going to clear the screen there. 76 00:05:01,630 --> 00:05:05,710 And if I start out the method just like before, I say Arrays.sort, 77 00:05:05,710 --> 00:05:10,260 I want to give it friends, and this is where I pass the comparator. 78 00:05:10,260 --> 00:05:15,400 The Comparator class has a static 79 00:05:15,400 --> 00:05:20,900 method named comparing, which will return a new comparator. 80 00:05:21,920 --> 00:05:24,650 The method's parameter expects you to define 81 00:05:24,650 --> 00:05:27,760 how to get the value that is to be compared. 82 00:05:27,760 --> 00:05:31,230 So this is most likely going to be a little new to you. 83 00:05:31,230 --> 00:05:36,680 But what we can do is use what is known as a method reference. 84 00:05:36,680 --> 00:05:40,570 We know that our elements in our friend array are all strings. 85 00:05:40,570 --> 00:05:43,580 And strings all have a method named length, so 86 00:05:43,580 --> 00:05:47,200 what we really want to do is to describe to this comparing method 87 00:05:47,200 --> 00:05:52,120 to use the length method on the string class to be what is being compared. 88 00:05:52,120 --> 00:05:57,010 To convey that, we type the name of the class, which was string, and 89 00:05:57,010 --> 00:06:00,410 then two colons, and then the name of the method. 90 00:06:00,410 --> 00:06:04,088 And the method, the instance method was length. 91 00:06:06,222 --> 00:06:09,520 Okay, and I'll close that final paren there. 92 00:06:09,520 --> 00:06:15,310 So that is saying, call the length method on each of these friends to compare them. 93 00:06:15,310 --> 00:06:18,310 Now, don't sweat it if that method reference is a bit unclear. 94 00:06:18,310 --> 00:06:20,410 We'll get to that, too, in a later course. 95 00:06:20,410 --> 00:06:26,980 And now if we run this, you'll see that I forgot to save my scratch.java. 96 00:06:26,980 --> 00:06:31,045 So I'm gonna press up a couple of times, open scratch.java, 97 00:06:31,045 --> 00:06:35,260 I'm going to clear the screen and run that one more time. 98 00:06:35,260 --> 00:06:38,480 Here we go, and if we look at our friends array now 99 00:06:38,480 --> 00:06:41,848 we will see that it is sorted by the length of people's names. 100 00:06:41,848 --> 00:06:45,580 Now Ben, as the shortest name, Alena, Pasan, and myself 101 00:06:45,580 --> 00:06:50,850 all have five letters in our name but Treasure has eight letters in her name. 102 00:06:50,850 --> 00:06:55,720 One of the cool things about comparators is you can easily change the order of it. 103 00:06:55,720 --> 00:06:59,330 Like let's say we wanna go longest first, we just change method, 104 00:06:59,330 --> 00:07:00,630 let's get that back. 105 00:07:00,630 --> 00:07:05,900 And so if I say, .reversed, here the comparator will go the other direction. 106 00:07:08,150 --> 00:07:11,480 And if we take a look at the friends right now, Treasure should be first and 107 00:07:11,480 --> 00:07:13,050 Ben should be last. 108 00:07:13,050 --> 00:07:16,590 Awesome, all right, so we got sorting skills. 109 00:07:16,590 --> 00:07:20,640 Let's take a look at ways to use and return arrays in our methods.