Grails - sort by the domain relation attribute (using createCriteria())

落爺英雄遲暮 提交于 2019-12-05 21:46:17

if you want to stick with the criteria approach, try something like this. Create an alias to you Action object and then access the property for sorting it

    def criteria = Task.createCriteria()
    def taskList = criteria.list {
        createAlias("actionParent","_action")
        order( "_action.actionName")
    }

I know this is an old question, but in my particular situation (wanting to sort on an association's association's field), it worked only when I passed the sort column directly into the list method:

def criteria = Task.createCriteria();
taskList = criteria.list(max: parameters.max, offset: parameters.offset, sort: parameters.sort, order: parameters.order) {
    // Other code here...
}

When I tried to put the ordering information in the closure itself, Hibernate barfed up an exception with a message org.hibernate.QueryException: could not resolve property: [Grails domain relation's relation's field] of: [Grails domain].

Have you tried using the listOrderBy* GORM command, so it would be something like

def tasksList = Task.listOrderByActionName()

try this

 def tasksList = Task.findAll()
 tasksListSorted = tasksList.sort { it.actionParent.actionName }

you can also do this

 tasksList.sort{
        task1, task2 -> task1.actionParent.actionName.compareToIgnoreCase(task2.actionParent.actionName)

    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!