title: JPA CriteriaQuery implements Spring Data Pageable.getSort()
body: <p>I used JPA CriteriaQuery to build my dynamic Query and pass in a Spring Data Pageable object:</p>
  
  <pre><code>sort=name,desc
  </code></pre>
  
  <p>At the backend I have a method in my Repository to support dynamic query:</p>
  
  <pre><code>public Page&lt;User&gt; findByCriteria(String username, Pageable page) {
      CriteriaBuilder cb = em.getCriteriaBuilder();
      CriteriaQuery&lt;User&gt; cq = cb.createQuery(User.class);
      Root&lt;User&gt; iRoot = cq.from(User.class);
      List&lt;Predicate&gt; predicates = new ArrayList&lt;Predicate&gt;();
  
      if (StringUtils.isNotEmpty(username)) {
          predicates.add(cb.like(cb.lower(iRoot.&lt;String&gt;get("username")), "%" + username.toLowerCase() + "%"));
      }
  
      Predicate[] predArray = new Predicate[predicates.size()];
      predicates.toArray(predArray);
  
      cq.where(predArray);
  
      TypedQuery&lt;User&gt; query = em.createQuery(cq);
  
      int totalRows = query.getResultList().size();
  
      query.setFirstResult(page.getPageNumber() * page.getPageSize());
      query.setMaxResults(page.getPageSize());
  
      Page&lt;User&gt; result = new PageImpl&lt;User&gt;(query.getResultList(), page, totalRows);
  
      return result;
  }
  </code></pre>
  
  <p>Note: I put only one param for demo purpose.</p>
  
  <p>However the returned data is unsorted therefore I would like to ask is that any way to implement Pageable in CriteriaQuery.</p>