Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting on multiple fields with criteria in Grails

I have the following query which id like to sorty by "raceDate" and by "raceNo" asc. I can figure out how to sort by one field, but not by two, any ideas?

def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 20,  100) 
        params.offset = params?.offset?.toInteger() ?: 0
        params.sort = "raceDate"
        params.order = params?.order ?: "asc"

        def results = Race.createCriteria().list(
                max: params.max,
                offset: params.offset,
                sort: params.sort, 
                order: params.order
                ) {
            and {
                if (params.raceNo && params.raceNo != '')
                    eq("raceNo", params.raceNo.toInteger())
                if (params.country && params.country != '')
                    eq("country", params.country)
                if (params.venue && params.venue != '')
                    eq("venue", params.venue)
                if (params.raceType && params.raceType != '')
                    eq("raceType", params.raceType)
                if (params.surface && params.surface != '')
                    eq("surface", params.surface)
                if (params.officialGoing && params.officialGoing != '')
                    eq("officialGoing", params.officialGoing)

                if (params.raceDateStart_year && params.raceDateStart_month && params.raceDateStart_day 
                        && params.raceDateEnd_year && params.raceDateEnd_month && params.raceDateEnd_day) {
                    String startInput = "${params.raceDateStart_year}/${params.raceDateStart_month}/${params.raceDateStart_day}"
                    Date startDate = jodaFormatter.parseDateTime(startInput).toDate();
                    String endInput = "${params.raceDateEnd_year}/${params.raceDateEnd_month}/${params.raceDateEnd_day}"
                    Date endDate = jodaFormatter.parseDateTime(endInput).toDate();  

                    between("raceDate", startDate, endDate)
                }
            }
        }

        [ raceInstanceList:results, raceInstanceTotal:results.totalCount, params:params ]
}
like image 368
catrapture Avatar asked Aug 20 '09 04:08

catrapture


1 Answers

Something like this should work:

def results = Race.createCriteria().list(
                max: params.max,
                offset: params.offset,
                sort: params.sort, 
                ) {
                order('raceDate', 'asc')
                order('raceNo', 'asc')
                and {
                    if (params.raceNo && params.raceNo != '')
                        eq("raceNo", params.raceNo.toInteger())
                ...
like image 146
Dave Klein Avatar answered Sep 21 '22 21:09

Dave Klein