1

我有一个组织类

class Organization {
hasMany = [member:Members]
}

class Members {
belongsTo = organization
}

我正在使用打印所有成员

<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>

我想对成员的打印进行排序,以便按字母顺序打印。有任何想法吗?

4

1 回答 1

4

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"])
于 2010-03-21T16:59:51.680 回答