How to compare String by their length in Java 7 and 8?

The natural way to compare String is the lexicographic way, which is implemented in the compareTo() method of String class, but sometimes you need to compare String by their length. You cannot use the default compareTo() method for that task, you need to write your own custom Comparator, which can compare String by length. Don't worry, It's easy to compare multiple String by their length, all you need to write is a Comparator which calculates their length using the length() method and compare them. Such comparator should return a positive value if first String has a length greater than second String, a negative value if the length of first String is less than the length of second String and zero if both String has the same length.
Read more »