Question

I have an organization class

class Organization {
hasMany = [member:Members]
}

class Members {
belongsTo = organization
}

I'm printing all the members using

<ol>
<g:each in="${organizationInstance?.members?}" var="m">
    <li><g:link controller="members" action="show" id="${m.id}">${m?.encodeAsHTML()}</g:link></li>
</g:each>
</ol>

I want to sort the printing of members so that it would print alphabetically. any ideas?

Was it helpful?

Solution

First, you need to change somehow your classes in order to have a name for members ! So let's assume that your classes are:

class Organization {
hasMany = [members:Member]
}

class Member {
belongsTo = organization
String name
}

Then you have two ways of sorting the members in alphabetical order.

First method : you can retrieve all members and then sort them as shown below:

<g:each in="${organizationInstance?.members?.sort {it.name} }" var="m">

Second Method : You retrieve members directly from GORM in alphabetical order

def members = Member.findAllByOrganization(organizationInstance, [sort: "name"])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top