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 a specific
0:05
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 the way that you want them to be.
0:15
All right, so a couple things, I'm gonna
set up scrratch.java here a little better.
0:20
So first I'm gonna do is gonna add
myself to this array here at the end,
0:25
so I'm always there with my friends.
0:29
And since it's getting a little bit
longer, I'm gonna do this typical thing
0:33
that you do with arrays,
is you put each item on its own line.
0:36
And because the curly braces are there,
it just worked.
0:41
Let's go ahead, let's get jshell started
too while we're doing this editing.
0:47
And the other thing I wanna do to show
off sorting properly is I'm gonna add
0:51
treasure here.
0:55
We'll add treasure to the list.
0:56
She's our new JavaScript teacher.
0:59
Okay, and we know that we're gonna be
using arrays static utility method.
1:02
So let's import it.
1:07
So in the scratch.java, we'll go ahead and
1:09
say import java.util.Arrays.
1:15
Okay, I've saved that file,
that's looking good, and
1:19
I'm going to go ahead and
open scratch.java.
1:23
And if we take a look at the current
ordering of our friends array,
1:30
we will see that they are out
of alphabetical order.
1:34
Awesome, so let's sort them.
1:38
So we have our arrays class and,
not surprisingly,
1:41
on that arrays class there
is a method called sort.
1:44
And that sort takes an array and
let's see what happens.
1:48
And if we take a look friends now, we'll
see that they're in alphabetical order.
1:53
It doesn't get much more clear than that,
does it?
1:59
I do want you to notice though,
2:02
that when we called this,
it didn't return a copy of the array.
2:03
It actually modified the array in place.
2:08
So that is something to be aware of.
2:12
Sorting does not return a copy,
it actually modifies the original array.
2:13
Wait a second, though, how do you think
it knew to compare them alphabetically?
2:19
Well, you might not know this, but
all strings have a compareto method.
2:24
Here, let's take a look really quick.
2:29
I'm gonna press Ctrl +
L to get back up here.
2:30
So if I have the string Apple, I can
call a method on it called compareTo,
2:33
and compare it to another string.
2:38
So let's take a look.
2:41
So what happens if I
compare apples to bananas?
2:42
I get -1.
2:46
So the way that this works is
if the item on the left is less
2:48
than the item on the right,
it returns a negative number.
2:52
Now, if it's equal it returns zero.
2:57
Like so, so if Apple compareTo Apple,
3:01
I'm not comparing apples and oranges here.
3:04
We get 0, right?
3:11
So, when it's equal.
3:12
But if I go the other way,
3:14
if I compare banana, compareTo Apple,
3:17
You'll see it returns a positive number,
meaning banana is greater that apple.
3:25
So our arrays sort method
uses this method to compare
3:31
each element in our array until
it's in the correct order.
3:35
In fact, you can sort an array
of any object as long as that
3:41
object marks itself as
being comparable and
3:45
provides a method called comparedTo
that returns these same values.
3:48
This -1 for less than, 0 for equals,
and a positive value for greater.
3:52
That is out of the scope of this course,
but we will get there shortly.
3:57
Check the teacher's notes for
more on interfaces and comparable.
4:00
For now, though, a consolation prize.
4:04
Let me show you off
something pretty powerful.
4:06
So let's say that we didn't wanna
sort our list alphabetically,
4:08
let's say we wanted to order our friends
by how many characters were in their name.
4:12
By default, the sort method uses
the compareTo method of objects, but
4:16
the sort method takes a second
parameter which is of type comparator.
4:21
So let's create one for our name length.
4:26
So first thing to do is to import
the comparator utility class.
4:29
I'll go ahead and do it up here.
4:34
Comparator.
4:41
And then in jshell if you go and
reopen a file.
4:44
Let's go ahead and reopen scratch.java.
4:47
And I'm going to clear the screen there.
4:57
And if I start out the method just
like before, I say Arrays.sort,
5:01
I want to give it friends, and
this is where I pass the comparator.
5:05
The Comparator class has a static
5:10
method named comparing,
which will return a new comparator.
5:15
The method's parameter
expects you to define
5:21
how to get the value
that is to be compared.
5:24
So this is most likely going
to be a little new to you.
5:27
But what we can do is use what
is known as a method reference.
5:31
We know that our elements in our
friend array are all strings.
5:36
And strings all have
a method named length, so
5:40
what we really want to do is to
describe to this comparing method
5:43
to use the length method on the string
class to be what is being compared.
5:47
To convey that, we type the name of
the class, which was string, and
5:52
then two colons, and
then the name of the method.
5:57
And the method,
the instance method was length.
6:00
Okay, and
I'll close that final paren there.
6:06
So that is saying, call the length method
on each of these friends to compare them.
6:09
Now, don't sweat it if that method
reference is a bit unclear.
6:15
We'll get to that, too, in a later course.
6:18
And now if we run this, you'll see
that I forgot to save my scratch.java.
6:20
So I'm gonna press up a couple of times,
open scratch.java,
6:26
I'm going to clear the screen and
run that one more time.
6:31
Here we go, and
if we look at our friends array now
6:35
we will see that it is sorted
by the length of people's names.
6:38
Now Ben, as the shortest name,
Alena, Pasan, and myself
6:41
all have five letters in our name but
Treasure has eight letters in her name.
6:45
One of the cool things about comparators
is you can easily change the order of it.
6:50
Like let's say we wanna go longest first,
we just change method,
6:55
let's get that back.
6:59
And so if I say, .reversed, here the
comparator will go the other direction.
7:00
And if we take a look at the friends
right now, Treasure should be first and
7:08
Ben should be last.
7:11
Awesome, all right, so
we got sorting skills.
7:13
Let's take a look at ways to use and
return arrays in our methods.
7:16
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