Sort collection in grail by date

I want to sort a collection in grails by date when I do the following:

def pics = Picture.findAllByChild(child, [sort: 'dateCreated', order: 'desc']) pics.add(Post.findAllByPostedToAll(true)) 

Because I added more items to the list, I need to sort by date again Created in descending order It does not look like the sort class can do this. I tried:

 pics.sort(it.dateCreated) 

But it is not allowed

+4
source share
2 answers

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

 pics.sort { it.dateCreated } 
+12
source

You can also change the default collation in the association.

In your image class class add:

 static mapping = { child(sort:'dateCreated', order:'desc') } 

This is not supported for one-to-many relationships, but works great for bidirectional relationships.

+2
source

Source: https://habr.com/ru/post/1434917/


All Articles