title: Criteria query by custom order by condition
body: <p>I am trying to order the data by field</p>
  
  <p>The query:</p>
  
  <pre><code>select * from user
  where (location = "%loc%" and role = "TECH")
  order by field (department, "ELEC", "MECH");
  </code></pre>
  
  <p>I created custom Order class for order by field its working fine only in single where condition if i provide two where condition Order by not working and returns 0 records.</p>
  
  <p>If I remove the order by it returns expected result.</p>
  
  <pre><code>USER TABLE
      ______________________________________________________
      |NAME    | AGE  | ROLE       | DEPARTMENT | LOCATION  |
      |________|______|____________|____________|___________|
      |name1   |  24  | TECH       | MECH       | location1 |
      |name2   |  25  | TECH       | ELEC       | location2 |
      |name3   |  26  | TECH       | COMP       | location3 |
      |name4   |  27  | NONTECH    | CVL        | location4 |
      -------------------------------------------------------
  
  *** EXPECTED RESULT ***
      ______________________________________________________
      |NAME    | AGE  | ROLE       | DEPARTMENT | LOCATION  |
      |________|______|____________|____________|___________|
      |name2   |  25  | TECH       | ELEC       | location2 |
      |name1   |  24  | TECH       | MECH       | location1 |
      |name3   |  26  | TECH       | COMP       | location3 |
      ------------------------------------------------------
  
  
  import java.util.List;
  
  import javax.persistence.criteria.CriteriaBuilder;
  import javax.persistence.criteria.Expression;
  import javax.persistence.criteria.Order;
  
  public class OrderByField implements Order {
  
      private Expression&lt;?&gt; expression;
  
      @Override
      public Order reverse() {
          return null;
      }
  
      @Override
      public boolean isAscending() {
          return true;
      }
  
      @Override
      public Expression&lt;?&gt; getExpression() {
          return expression;
      }
  
      public OrderByField(CriteriaBuilder cb, List&lt;Expression&lt;?&gt;&gt; list) {
          this.expression = cb.function("field", String.class, list.toArray(new Expression&lt;?&gt;[list.size()]));
      }
  }
  
  </code></pre>
  
  <p><strong>Criteria logic</strong></p>
  
  <pre><code>    CriteriaBuilder builder = em.getCriteriaBuilder();
      CriteriaQuery&lt;User&gt; criteriaQuery = builder.createQuery(User.class);
      Root&lt;User&gt; userData = criteriaQuery.from(User.class);
  
      //Order By field DEPARTMENT
      List&lt;Expression&lt;?&gt;&gt; departmentList = new ArrayList&lt;&gt;();
      departmentList.add(userData.get("department"));
      departmentList.add(builder.parameter(String.class, "param1"));
      departmentList.add(builder.parameter(String.class, "param2"));
  
      OrderByField order = new OrderByField(builder, departmentList); //custom class for ordeBy field
  
      //WHERE CLUASE for location and role
  
      List&lt;Predicate&gt; predicates = new ArrayList&lt;&gt;();
      predicates.add(builder.like(userData.get("location"), "%" + "loc" + "%"));
      predicates.add(builder.like(userData.get("role"), "%" + "TECH" + "%"));
  
      criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
      criteriaQuery.orderBy(order);
  
      TypedQuery&lt;User&gt; query = em.createQuery(criteriaQuery);
  
      //ORDER BY FIELD PARAMS
      query.setParameter("param1", "ELEC");
      query.setParameter("param2", "MECH");
  
      return query.getResultList();
  
  </code></pre>
  
  <p>** User Entity **</p>
  
  <pre><code> public class User {
  
          @Column(name =  "NAME")
          private String name;
  
          @Column(name =  "AGE")
          private String age;
  
          @Column(name =  "ROLE")
          private String role;
  
          @Column(name =  "DEPARTMENT")
          private String department;
  
          @Column(name =  "LOCATION")
          private String location;
  }
  
  </code></pre>