Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Arrays!
You have completed Java Arrays!
Preview
Arrays can be ordered through sorting. Let's explore how!
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In addition to copying, the Arrays utility
class also lets you sort an array.
0:00
Now this is super handy in situations
where you need
0:05
a specific order to the elements
in your array.
0:08
In contrast to adding elements, let's
see if I can renew your love for arrays by
0:11
showing off how easy they are to arrange
in a way that you want them to be.
0:15
Alright, so a couple of things.
0:21
I'm going to set up
Scratch.java here a little better.
0:23
Let's go ahead and get JShell started too
while we're doing this editing.
0:26
So first what I'm going to do
is I'm going to add myself
0:30
to this array here at the end,
so I'm always there with my friends.
0:32
And since it's getting a little bit
longer, I'm going to do this
0:37
typical thing that you do with arrays, is
you put each item on its own line.
0:39
And because the curly braces are there,
it works.
0:44
And another thing I want to do to show off
sorting properly
0:47
is I'm going to add Yaroslav
to the list here in the beginning,
0:50
and I'm going to add Dan to the end.
0:55
Okay, and we know that we're going to be
0:59
using Array's static utility method,
so let's import it.
1:00
So in scratch.java,
we'll go ahead and say import
1:04
java.util.arrays.
Okay, save that file, that's looking good
1:07
And I'm going to go ahead and open
scratch.java in JShell here. And
1:14
if we take a look at the current ordering
of our friends array, we'll see that
1:24
they're out of alphabetical order. Awesome
so let sort them.
1:27
So we have our arrays class
1:32
and not surprisingly on that Arrays class
there is a method called Sort.
1:33
And that takes an array
so friends. And let's see what happens.
1:38
If we take a
1:43
look at friends now, we'll see that
they're in alphabetical order.
1:43
It doesn't get much more clear
than that, does it?
1:46
I do want you to notice though,
that when we called this,
1:49
it didn't return a copy of the array.
1:52
It actually modified the array in place.
1:54
So that is something to be aware of.
1:56
Sorting does not return a copy,
it actually modifies the original array.
1:59
Wait a second, though.
2:03
How do you think it knew to compare them
alphabetically?
2:04
Well, you might not know this,
but all strings have a compareTo method.
2:08
Here, let's take a look really quick.
2:12
I'm going to press Ctrl-L
to get back up here.
2:14
So if I have the string apple,
I can call a method on it
2:18
called compareTo,
and compare it to another string.
2:21
So let's take a look.
2:25
So what happens
if I compare apple to banana?
2:27
I get negative one.
2:30
So the way that this works is
if the item on the left is less
2:32
than the item on the right,
it returns a negative number.
2:35
Now if it's equal, it returns zero.
2:39
so if I compare apple to apple, I'm
2:41
not comparing apples to oranges
here, we get zero, right?
2:45
But if I go the other way,
2:50
if I compare banana to apple,
2:51
you'll see it returns a positive number,
2:56
meaning that banana is greater than apple.
2:58
So our array sort method uses this method
3:01
to compare each element in our array
until it's in the correct order.
3:03
In fact, you can sort
an array of any object
3:07
as long as that object marks itself
as being comparable and provides
3:10
a method called compareTo that returns
these same values: This negative
3:14
one for less than, zero for equals,
and a positive value for greater.
3:19
That's out of the scope of this course,
but we'll get there shortly.
3:23
Check the teacher's notes
for more on interfaces and comparable.
3:26
For now, though, a consolation prize.
3:30
Let me show you something pretty powerful.
3:33
So let's say that we didn't
want to sort our list alphabetically.
3:36
Let's say we wanted to order our friends
by how many characters were in their name.
3:39
By default, the sort method uses
the compareTo method of objects.
3:44
But the sort method takes a second
parameter, which is of type comparator.
3:48
So let's create one for our name length.
3:53
So the first thing to do is import
the comparator utility class.
3:55
I'll go ahead and do it up here.
4:00
Comparator, and save.
4:05
And then in JShell,
let's go ahead and reopen scratch.java.
4:09
And I'm going to clear the screen
there too.
4:13
If I start out the method just like before,
4:16
I say arrays.sort.
4:18
I want to give it friends.
4:21
And this is where I pass the comparator.
4:23
The comparator class has a static
method named comparing,
4:25
which will return a new comparator.
4:29
The methods parameter expects you
to define how to get the value
4:32
that is to be compared.
So this is most likely going to be
4:36
a little new to you. But what we can do is
use what is known as a method reference.
4:39
We know that our elements in our friend
array are all strings
4:45
and strings
all have a method named length.
4:48
So what we really want to do
is to describe to this comparing method
4:51
to use the length method on each string
class to be what is to be compared.
4:55
To convey that, we type
the name of the class, which is string,
5:00
and then two colons,
and then the name of the method.
5:04
and the instance method was length.
5:08
Okay, I'll close that
with the final paren there.
5:11
So that is saying, call the length method
on each of these friends to compare them.
5:14
Now, don't sweat it
if that method reference is a bit unclear.
5:19
We'll get to that too in a later course.
5:22
And now if we run this, we'll see that
5:24
friends is sorted
by the length of the people's names.
5:26
Dan ended up first because he only has
three letters in his name,
5:29
then Brian and Laura
because they each have five,
5:32
then Rohald and I
because we each have six, and Yaroslav
5:35
went from first to the last position
because he has eight letters in his name.
5:39
One of the cool
5:43
things about comparators
is you can easily change the order of it.
5:44
Let's say we want to go longest first.
5:48
We just change the method.
5:50
Let's get that back.
5:52
And so if I say dot reversed, here
5:56
the comparator will go
the other direction.
5:59
And if we take a look at friends now,
6:03
Yaroslav should be first
and Dan should be last.
6:05
Awesome. Alright,
so we got sorting skills.
6:08
Let's take a look at ways
to use and return arrays in our methods.
6:12
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up