The first thing that comes to mind when sorting is the use of a comparator.
In this case, the solution should compare lengths of strings, and so the integers are returned by calling String.length() for each string in the given array. So, if the integers are sorted (ascending or descending), then the strings will be sorted.
The Java Arrays class already provides a sort() method that takes the array to sort and a comparator. In this case, Comparator<String> should do the job.
The following is a method that sorts the given array by relying on the Arrays.sort() method:
public static void sortArrayByLength(String[] strs, Sort direction) {
if (direction.equals(Sort.ASC)) {
Arrays.sort(strs, (String s1, String s2)
-> Integer.compare(s1.length(), s2.length()));
} else {
Arrays.sort(strs, (String s1, String s2)
-> (-1) * Integer.compare(s1.length(), s2.length()));
}
}
Starting with Java 8, the Comparator interface was enriched with a significant number of useful methods. One of these methods is comparingInt(), which takes a function that extracts an int sort key from the generic type and returns a Comparator<T> value that compares it with that sort key. Another useful method is reversed(), which reverses the current Comparator value.
Based on these two methods, we can empower Arrays.sort() as follows:
public static void sortArrayByLength(String[] strs, Sort direction) {
if (direction.equals(Sort.ASC)) {
Arrays.sort(strs, Comparator.comparingInt(String::length));
} else {
Arrays.sort(strs,
Comparator.comparingInt(String::length).reversed());
}
}
The solutions we've presented here return void, which means that they sort the given array. To return a new sorted array and not alter the given array, we can use Java 8 functional style, as shown in the following snippet of code:
public static String[] sortArrayByLength(String[] strs,
Sort direction) {
if (direction.equals(Sort.ASC)) {
return Arrays.stream(strs)
.sorted(Comparator.comparingInt(String::length))
.toArray(String[]::new);
} else {
return Arrays.stream(strs)
.sorted(Comparator.comparingInt(String::length).reversed())
.toArray(String[]::new);
}
}
So, the code creates a stream from the given array, sorts it via the sorted() stateful intermediate operation, and collects the result in another array.