title: HQL case in ORDER BY not sorting correctly
body: <p>I have an HQL that I can choose to order by "studentNumber" or its name.</p>
  
  <pre><code>    String orderBy = orderByNumber
              ? "(case when e.studentNumber is null then e.student.name else e.studentNumber end)" : "e.student.name";
  </code></pre>
  
  <p>But when if I choose studentNumber, and it is null, I should order by name. For that I used:</p>
  
  <pre><code>   (case when e.studentNumber is null then e.student.name else e.studentNumber end)
  </code></pre>
  
  <p>This works fine, but when I have more than 10 students the order comes like this:</p>
  
  <pre><code>    1
      10
      11
      12
      2
      3
      4
      5
      6
      7
      8
      9
  </code></pre>
  
  <p><code>studentNumber</code> is set as Integer in the database and entity. I guess this would happen if it was considered a string.</p>
  
  <p>thank you</p>
  
  <p><strong>EDIT 1</strong> I tried this new query:</p>
  
  <pre><code>    (case when (case when e.studentNumber is null then e.student.name else e.studentNumber end) = e.studentNumber then cast(e.studentNumber as int) end)
  </code></pre>
  
  <p>now it orders correctly, but when e.studentNumber is null, is not ordering by name. If I add the then and try this:</p>
  
  <pre><code>    (case when (case when e.studentNumber is null then e.student.name else e.studentNumber end) = e.studentNumber then cast(e.studentNumber as int)  else e.student.name end)
  </code></pre>
  
  <p>The first problem comes back from the dead as a zombi (1, 10, 2, 3)...</p>
  
  <p><strong>EDIT 2</strong></p>
  
  <p>I also tried with coalesce:</p>
  
  <pre><code>   (case when coalesce(e.studentNumber, e.student.name) = e.studentNumber then cast(e.studentNumber as int) else e.student.name end)
  </code></pre>
  
  <p>Same as above, in both cases.</p>