Search code examples
scalasortingplayframeworkplayframework-2.2templating

How to keep a groupedby list sorted in Play Framework 2 templates


I've got a list of complex objects that I want to display, grouped by one of its attribute in a Play 2 template.

I managed to do it :

@measures.groupBy(_.question.category).map {
    case (category, items) => {
         // Category stuff
         @for(item <- items) {
             // List of items
         }
    }   
}

The problem is that the list was sorted in my Java controller, but the keyset of the map that I create is not sorted anymore (I would like to sort the key set using something like _.question.category.order).

Is there a way to have a sorted map on this attribute?

Thanks !


Solution

  • What type is measures? Did you try to use a LinkedHashSet? This should keep the elements order in contrast to e.g. a HashSet.