views:

73

answers:

3

Hi,

I have two domain classes with 1:n relationship:

import Action

class Task {    
    Action actionParent
    String taskName
}

and

class Action {
    String actionName
}

I have the list of Tasks where I have the column "Action name", I would like to sort this column by Action.actionName. Now I'm using the createCriteria() method [I need to use it because I have more logic for filtering and sorting...], but I'm able to sort only by "Action.id". This method looks like:

def criteria = Task.createCriteria();
taskList = criteria.list {
    if(parameters.max != null)
        maxResults(parameters.max)
    if(parameters.offset != null)
        firstResult(new Integer(parameters.offset))
    if(parameters.sort != null && parameters.order)
        order(parameters.sort, parameters.order)
}

Is there any way to sort the domain class data by the relationship attributes?

Thanks for any replay,

Mateo

A: 

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

def tasksList = Task.listOrderByActionName()
MTH
A: 

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")
    }
Aaron Saunders
A: 

Hey, the createAlias() work fine for such sorting. I'm just wondering that I'm not able to find it in the documentation:-) But nevertheless, thank you both. Mateo

Mateo