In this tutorial, we are going to look at sorting lists using
the Comparator interface. This is very simple and this is one of the very good
examples of how to use anonymous classes.
Now before we start, I would like to state that all the
members of the collections framework are children classes of the Collections
class. The Collections class has some methods, and the one that is of most
interest to us with regards to this tutorial is the sort() method.
Now, let is first look at how to sort lists that contain
inbuilt Java datatypes such as strings and integers.
Sorting Lists with Inbuilt Datatypes
a) Sorting Integers in Ascending Numerical Order
So let’s create a list of integers, add a number of elements
and then sort it in ascending order:
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(9);
numbers.add(1032);
numbers.add(105);
numbers.add(87);
To sort the above list, we use the sort() method, which is
part of the Collections class like this:
Collections.sort(numbers);
Then, we output the list using a foreach loop like this:
for(Integer number: numbers) {
System.out.println(number);
}Figure 1: Using the sort() method in the Collections class to sort a list of numbers
Now, you see that the list is sorted in ascending numerical
order.
b) Sorting Strings in Alphabetical Order
So let’s create a list of vegetables, add a number of
elements and then sort it in alphabetical order:
List<String> vegetables = new ArrayList<String>();
vegetables.add(“onion”);
vegetables.add(“kales”);
vegetables.add(“spinach”);
vegetables.add(“broccoli”);
To sort the above list, we use the sort() method, which is
part of the Collections class like this:
Collections.sort(vegetables);
Then, we output the list using a foreach loop like this:
for(String vegetable: vegetables) {
System.out.println(vegetable);Figure 2: Using the sort() method in the Collections class to sort a list of strings
Now, you see that the list is sorted in alphabetical order.
c) Sorting Integers in Descending Numerical Order
So let’s create a list of integers, add a number of elements
and then sort it in ascending order:
List<Integer> numbers = new ArrayList<Integer>();
numbers.add(9);
numbers.add(1032);
numbers.add(105);
numbers.add(87);
To sort the above list, we use the sort() method, which is
part of the Collections class like. However, since we want the list to be
sorted in reverse numeric order, we have to add some functionality when we call
Collections.sort(). What we do, is supply a constructor of the Comparator
interface and within it, we override the compare method like this:
Collections.sort(numbers, new Comparator<Integer>(){
@Override
public int
compare (Integer int1, Integer int2) {
return
–int1.compareTo(int2);
};
The negative sign after the “return” keyword tells Java that
we want it to sort the list in reverse numeric order.
Then, we output the list using a foreach loop like this:
for(Integer number: numbers) {
System.out.println(number);
}Figure 3: Using the sort() method in the Collections class to sort a list of numbers in reverse numerical order
Now, you see that the list is sorted in descending numerical
order.
d) Sorting Strings in Reverse Alphabetical Order
So let’s create a list of vegetables, add a number of elements
and then sort it in ascending order:
List<String> vegetables = new
ArrayList<String>();
vegetables.add(“onion”);
vegetables.add(“kales”);
vegetables.add(“spinach”);
vegetables.add(“broccoli”);
To sort the above list, we use the sort() method, which is part
of the Collections class. However, since we want the list to be sorted in
reverse alphabetic order, we have to add some functionality when we call
Collections.sort(). What we do, is supply a constructor of the Comparator
interface and within it, we override the compare method like this:
Collections.sort(vegetables, new
Comparator<String>(){
@Override
public
int compare (String vegetable1, String vegetable2) {
return
-vegetable1.compareTo(vegetable2);};
});
The negative sign after the “return” keyword tells Java that
we want it to sort the list in reverse alphabetical order.
Then, we output the list using a foreach loop like this:
for(String vegetable: vegetables) {
System.out.println(vegetable);
}Figure 4: Using the sort() method in the Collections class to sort a list of numbers in reverse numerical order
Now, you see that the list is sorted in descending alphabetical order.
e) Sorting Strings based on Length
So let’s create a list of vegetables, add a number of
elements and then sort it in ascending order:
List<String> vegetables = new
ArrayList<String>();
vegetables.add(“onion”);
vegetables.add(“kales”);
vegetables.add(“spinach”);
vegetables.add(“broccoli”);
To sort the above list, we use the sort() method, which is
part of the Collections class. However, since we want the list to be sorted based
on the length of the string, we have to add some functionality when we call
Collections.sort(). What we do, is supply a constructor of the Comparator
interface and within it, we override the compare method like this:
Collections.sort(vegetables, new Comparator<String>(){
@Override
public int
compare (String vegetable1, String vegetable2) {
int
len1 = vegetable1.length();
int
len2 = vegetable2.length();
if
(len1 > len2) {
return
1;
}
else
if (len1 < len2) {
return
-1;
}
else
return 0;
}
});
Then, we output the list using a foreach loop like this:
for(String vegetable: vegetables) {
System.out.println(vegetable);
}Figure 5: Sorting strings based on their length
Now, you see that the list is sorted based on the length of
the strings.
If you have any questions or comments, please leave them in
the comments section below and I will address them.
No comments:
Post a Comment