Round hill of the apples - Groovy, Grails, Griffon and a hint of Java

  • Blog
  • About
  • Contact us
 

Identified Flying Objects - no worries, I got it sorted

04/04/2012

1 Comment

 
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()}


 


Comments

Guinevere Keller link
06/24/2012 17:50

I stumbled on this from Google and wanted to say thanks for posting

Reply



Leave a Reply

    Author

    Groovy & Java developer at FunnelScope.com

    Archives

    April 2012

    Categories

    All
    Groovy Collections
    Spring Groovy Dependency Injection

    RSS Feed


Create a free website with Weebly