Ordering using Grails CreateCriteria

14 / May / 2010 by Imran Mir 2 comments

Recently, in my project I had to implement search functionality. I used grails createCriteria to implement it.Now I needed to apply the sorting on the result returned. My domain was something like this :

class MyEntity {

    OrganizationName orgName
    PersonName personName

    static constraints = {
      orgName('nullable', true)
      personName('nullable', true)
    }
  }

  class OrganizationName {

    String organizationName

  }

  class PersonName {

    String familyname
    String firstName

  }

The “MyEntity” could either be a person or an organization. In the view, we show just the name, and that could be either the organization-name or person-name. The list needed to be sorted by the name,irrespective of the fact, whether it is a name of a person or an organization. Grails “createCriteria” did the magic for me. I wrote something like this :

  MyEntity.createCriteria ().list {

    orgName {
      order('name')
    }
    personName {
      order('familyName')
    }
  }

The ordering seems to ignore nulls. It sorts the MyEntity objects with orgName, groups them togethor and then sorts the MyEntity objects with personName and makes another group for them and then returns a combined, grouped sorted list of both. Hats off to grails.

Imran Mir
imran@intelligrape.com

FOUND THIS USEFUL? SHARE IT

comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *