Question

I have two domain models:

class Resource{
   String name

   static mapping = {
                 sort name:"asc"
          }
}

class ResourceGroup{
   String groupName

   static hasMany = [resources: Resource]
}

controller:

def resGroups = ResourceGroup.findAll()
render (
        view: "index",
        model: [resourcegroups: resGroups]
)

so and now in my gsp:

<g:each in="${resourcegroups}" var="item" status="i">
   ...
   <g:each in="${item.resources}" var="res" status="y">
       <!-- THESE ITEM.RESOURCES ARE UNSORTED! -->
   </g:each>
   ...
</g:each>

my Question is how can I sort this "item.resources"? this is a persistent set of hibernate! I thought this could be handled with the mapping sort name: 'asc', but it doesn't work :-(

Was it helpful?

Solution

You cannot have a default sort on a one-to-many or many-to-many relationship. See documentation here, paying particular attention to the note at the bottom that says:

These mappings will not work for default unidirectional one-to-many or many-to-many relationships because they involve a join table. See this issue for more details. Consider using a SortedSet or queries with sort parameters to fetch the data you need.

The default sort you have specified actually does sort a list of Resource objects (i.e. if you got the list like this Resource.getAll() the list would be sort in the order you specified).

To do what you want consider creating a tab lib as Don suggests here.

OTHER TIPS

try item.resources.sort { it.name } for "asc",

or item.resources.sort { it.name }.reverse() for "desc".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top