Sagarmichael
Sagarmichael

Reputation: 1624

Sort a collection in grails by date

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

Upvotes: 6

Views: 13334

Answers (2)

Jay Prall
Jay Prall

Reputation: 5465

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.

Upvotes: 2

doelleri
doelleri

Reputation: 19682

the sort method takes a closure argument, so the correct call (with implicit parens) is

pics.sort { it.dateCreated }

Upvotes: 17

Related Questions