Ordering using Grails CreateCriteria « Intelligrape Groovy & Grails Blogs

Ordering using Grails CreateCriteria

Posted by

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

This entry was posted on May 14th, 2010 at 11:43 pm and is filed under Grails, Groovy . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Ordering using Grails CreateCriteria”

  1. I like this blog very much so much fantastic info.

  2. paypal says:

    Pingback from topsy.comTwitter Trackbacks for ASPCA Pet Insurance | In the News: The Physics Behind a Dog’s … http://www.allpet.co.uk [aspcapetinsurance.com] on Topsy.com

  3. An outstanding share! I’ve just forwarded this onto a coworker who has been doing a little homework on this. And he actually bought me lunch due to the fact that I discovered it for him… lol. So allow me to reword this…. Thanks for the meal!! But yeah, thanks for spending the time to discuss this topic here on your internet site.

Leave a Reply