Search code examples
grailsgrails-ormgsp

Defining default sort-order in Grails/GORM


Let's say I have definied a User object using GORM. Each user can have zero or more Login:s. Each Login has a timestamp. When retrieving user.logins I want the logins to be sorted based on the value of login.date. What is the correct Grails way to achieve this?

Example: I want the following code to list all the user's logins in ascending order.

<g:each var="login" in="${user.logins}">
  <tr>
    <td>${login.date}</td>
  </tr>
</g:each>

These are the referenced classes:

class User {
  ...
  def hasMany = [logins: Login]
  static fetchMode = [logins: "eager"]
}

class Login {
  Date date
  ...
  def belongsTo = [User]
}

I'm running Grails 1.0.4 which is the latest stable release.


Solution

  • The handling of default sort order in Grails/GORM seems to have been radically simplified in Grails 1.1: