Comparator example: sorting Strings by length
We introduced the notion of a Comparator in Java, which is
used to specify the sort order of a particular class. Let's start with an example to sort Strings
by their length. We thus want to write a Comparator that compares Strings. So the
general format of our Comparator will be as follows:
public class StringLengthComparator implements Comparator<String> {
public int compare(String o1, String o2) {
return ...
}
}
Now, we just need to make the compare() return method return an appropriate value to
indicate the ordering. Effectively, the logic of this method should be o1.compareTo(o2).
That is– as discussed when we reviewed sorting_comparable_compareto.shtml the compareTo()
method– it should return:
- a negative number if (and only if) o1 comes before o2;
- a positive number if (and only if) o1 comes after o2;
- else 0.
So a simple implementation would be1:
public class StringLengthComparator implements Comparator<String> {
public int compare(String o1, String o2) {
if (o1.length() < o2.length()) {
return -1;
} else if (o1.length() > o2.length()) {
return 1;
} else {
return 0;
}
}
}
Now, armed with our Comparator and some vital string data that we need ordering by length:
String[] strs = {"boxer shorts", "grundies", "boxers",
"elasticated Y-fronts", "underpants", "briefs"};
we can sort our string data by length with a simple call to Arrays.sort(), this time
passing in an instance of our parameter:
// Sort
Arrays.sort(strs, new StringLengthComparator());
// Print the results
for (String s : strs)
System.out.println(s);
And lo and behold, our garments are sorted in correct lengthwise order:
boxers
briefs
grundies
underpants
boxer shorts
elasticated Y-fronts
Note that, as discussed in our section on the Java sort algorithm,
the sort is stable. That is, strings with the same length are guaranteed not to be reversed,
and so our boxers correctly precede our briefs.
1. Some programmers prefer the more succinct version
return o1.length() < o2.length() ? -1 : (o1.length() > o2.length() ? 1 : 0);.
It is also presumably safe to use a subtraction–
on 32-bit architecture at least, it's impossible to have Java Strings long enough
to overflow when doing the subtraction!
If you enjoy this Java programming article, please share with friends and colleagues. Follow the author on Twitter for the latest news and rants.
Editorial page content written by Neil Coffey. Copyright © Javamex UK 2021. All rights reserved.