I am wanting to sort a collection in grails by date I am currently doing the following:
def pics = Picture.findAllByChild(child, [sort: 'dateCreated', order: 'desc'])
pics.add(Post.findAllByPostedToAll(true))
Because I have added more items to the list i need to sort again by dateCreated descending It doesn't look like the sort class can do this. I have tried:
pics.sort(it.dateCreated)
But this is not allowed
the sort
method takes a closure argument, so the correct call (with implicit parens) is
pics.sort { it.dateCreated }
You can also change the default sort on the association.
In your Picture domain class add:
static mapping = {
child(sort:'dateCreated', order:'desc')
}
This is not supported for unidirectional one to many relationships, but works great for bidirectional ones.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With