Get smallest string in list

This example will show how to find the shortest or minimum length string contained within a list using groovy. In snippet below we will reference Katie Perry song titles where we will use a closure to create a Comparator to sort by the length of a string. The <=> operator or spaceship operator will return -1 if left is smaller 0 if == to right or 1 if greater than the right and in this instance will sort the collection by smallest to greatest. A comparable example we demonstrate how to get the smallest length string in a collection using java, java 8 and guava.

Using sort

@Test
void smallest_string_arraylist() {

    def collection = [
            "Dark Horse",
            "Roar",
            "This is how we do",
            "Firework",
            "Birthday"]

    assertEquals("Roar",
            collection.sort({ a, b -> a.length() <=> b.length() })[0])
}