0

I have the following scenario (Domain classes)

class Request { 
    String title                
    List statusUpdates

    static hasMany = [statusUpdates: StatusUpdate]
}

and

class StatusUpdate {

    Date dateCreated
    String statusFrom
    String statusTo
    String notes

    static belongsTo = Request
}

I am currently using createCriteria to implement filtering and basic sorting of Request.

Now I want to get a list of Requests starting from the most recently updated (ordered by the value of the field dateCreated of the last StatusUpdate for the Request)

Any hint?

Danilo
  • 2,676
  • 7
  • 32
  • 36
  • knock yourself out https://github.com/vahidhedayati/ajaxdependancyselection/blob/master/grails-app/services/ajaxdependancyselection/AutoCompleteService.groovy loads of examples here – V H Feb 13 '15 at 09:49

2 Answers2

1

assuming that every Request has at least one StatusUpdate and slightly changing belongsTo declaration, you could go the other direction:

class StatusUpdate {

    Date dateCreated
    String statusFrom
    String statusTo
    String notes

    static belongsTo = [req: Request] // I assume that naming the field "request" would cause whole bunch of weird problems, therefore "req"
}

StatusUpdate.createCriteria().list() {
    req {
        // your criteria for requests filtering
    }
    projections {
        groupProperty 'req', 'reqAlias'
        max 'dateCreated', 'maxDateCreatedAlias'
    }
    order 'maxDateCreatedAlias', 'desc'
}.collect { it[0] }
0

You can use Comparator or Comparable interface. Here you have a nice example link

Szymon Roziewski
  • 956
  • 2
  • 20
  • 36
  • Is this applicable if the instances are retrieved from a database (GORM) with other arbitrary criterias (filters)? – Danilo Feb 16 '15 at 07:51