How to Alphabetically retrieve members of a list?
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?
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"])
精彩评论