Tired of implementing Comparable? sick of creating Comparators?
In my case I had a Map full of complex objects. Each of my complex objects held an arraylist of photo albums. As I was going to be iterating over the map later on I wanted to be able to get the elements out of the map ordered by the number of photoalbums in each element value - in descending order:
Groovy to the rescue with the spaceship operator (<=>) and a simple one liner:
friendActivityBucketMap = friendActivityBucketMap.sort {entry1,entry2 -> entry2.value.photoAlbums.size() <=> entry1.value.photoAlbums.size()}
In my case I had a Map full of complex objects. Each of my complex objects held an arraylist of photo albums. As I was going to be iterating over the map later on I wanted to be able to get the elements out of the map ordered by the number of photoalbums in each element value - in descending order:
Groovy to the rescue with the spaceship operator (<=>) and a simple one liner:
friendActivityBucketMap = friendActivityBucketMap.sort {entry1,entry2 -> entry2.value.photoAlbums.size() <=> entry1.value.photoAlbums.size()}