You can do this, but it requires two queries. This is because you need to use a group to sort by the size of the collection, but this requires that you list all of your properties. If you add or remove one request, it will break. Thus, the solution is to run one query that finds ordered identifiers, and the second - instances for these identifiers:
String hql = '''
SELECT t.id
FROM Topic t LEFT JOIN t.posts AS post
GROUP BY t.id
ORDER BY COUNT(post) DESC
'''
def ids = Topic.executeQuery(hql)
def orderedTopics = Topic.getAll(ids)
source
share