title: How to order by count() in JPA
body: <p>I am using This JPA-Query:</p>
  
  <pre><code>SELECT DISTINCT e.label FROM Entity e 
  GROUP BY e.label 
  ORDER BY COUNT(e.label) DESC
  </code></pre>
  
  <p>I get no errors and the results are sorted almost correct but there are some values wrong (either two values are flipped or some single values are completly misplaced)</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Adding COUNT(e.label) to my SELECT clause resolves this problem for this query.</p>
  
  <p>But in a similar query which also contains a WHERE clause the problem persists:</p>
  
  <pre><code>SELECT DISTINCT e.label, COUNT(e.label) FROM Entity e 
  WHERE TYPE(e.cat) = :category 
  GROUP BY e.label 
  ORDER BY COUNT(e.label) DESC
  </code></pre>