title: Using groupProperty and countDistinct in Grails Criteria
body: <p>I'm using Grails 1.2.4. I would like to know on how can I sort by "countDistinct" (descending) and with groupProperty inside a projections.</p>
  
  <p>Here are my domains:</p>
  
  <pre><code>class Transaction {
  
      static belongsTo = [ customer : Customer, product : Product ]
  
      Date transactionDate = new Date()
  
      static constraints = {
          transactionDate(blank:false)    
      }
  
  }
  
  class Product {
  
      String productCode
  
      static constraints = {
          productCode(blank:false)    
      }
  }
  </code></pre>
  
  <p>In MySQL terms, this is what I want:</p>
  
  <pre><code>select 
      product_id,
      count(product_id)
  from
      transaction
  group by
      product_id
  order by
      count(product_id) desc
  </code></pre>
  
  <p>In general term, I would like to get a list of products (or just product id) sorted by the number of transactions a product had (descending)</p>
  
  <p>This is my guess:</p>
  
  <pre><code>def c = Transaction.createCriteria() def transactions = c.list {
      projections {
          groupProperty("product")
          countDistinct("product")
      }
      maxResults(pageBlock)
      firstResult(pageIndex) }
  
  def products = transactions.collect { it[0] }
  </code></pre>
  
  <p>But it doesn't give my expected result. Any lead on this will be highly appreciated. Thanks!</p>