title: Spring-data & Redis: sort the results
body: <p>From a Spring boot application, I want to use Redis and retrieve paged and sorted results.</p>
  
  <p>Maven config:</p>
  
  <pre><code>&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.data&lt;/groupId&gt;
    &lt;artifactId&gt;spring-data-redis&lt;/artifactId&gt;
    &lt;version&gt;2.1.4.RELEASE&lt;/version&gt;
  &lt;/dependency&gt;
  
  &lt;dependency&gt;
    &lt;groupId&gt;redis.clients&lt;/groupId&gt;
    &lt;artifactId&gt;jedis&lt;/artifactId&gt;
    &lt;version&gt;2.10.2&lt;/version&gt;
    &lt;type&gt;jar&lt;/type&gt;
  &lt;/dependency&gt;
  </code></pre>
  
  <p>Based on an online example, I implemented the following repository:</p>
  
  <pre><code>@Repository
  public interface StudentRepository extends PagingAndSortingRepository&lt;Student, String&gt; {
  
    List&lt;Student&gt; findByName(String Name, Pageable page);
    List&lt;Student&gt; findByName(String Name);
  </code></pre>
  
  <p>}</p>
  
  <p>Now I want to filter my Student using</p>
  
  <pre><code>studentRepository.findAll(Sort.by("range").ascending());
  </code></pre>
  
  <p>With Student:</p>
  
  <pre><code>@RedisHash("Student")
  public class Student implements Serializable {
    @Id
    private String id;
    @Indexed
    private String name;
    private Gender gender;
    private int grade;
  </code></pre>
  
  <p>Whatever the sort is, the order is always the same. After a debug session, I understood that the reason is:</p>
  
  <pre><code>class RedisQueryEngine extends QueryEngine&lt;RedisKeyValueAdapter, RedisOperationChain, Comparator&lt;?&gt;&gt; {
  
  /**
   * Creates new {@link RedisQueryEngine} with defaults.
   */
  RedisQueryEngine() {
      this(new RedisCriteriaAccessor(), null);
  }
  
  /**
   * Creates new {@link RedisQueryEngine}.
   *
   * @param criteriaAccessor
   * @param sortAccessor
   * @see QueryEngine#QueryEngine(CriteriaAccessor, SortAccessor)
   */
  private RedisQueryEngine(CriteriaAccessor&lt;RedisOperationChain&gt; criteriaAccessor,
          @Nullable SortAccessor&lt;Comparator&lt;?&gt;&gt; sortAccessor) {
      super(criteriaAccessor, sortAccessor);
  }
  </code></pre>
  
  <p>The public constructor that is called by Spring has no sortAccessor defined. This sort accessor is then used in the QueryEngine class by my query:</p>
  
  <pre><code>public &lt;T&gt; Collection&lt;T&gt; execute(KeyValueQuery&lt;?&gt; query, String keyspace, Class&lt;T&gt; type) {
  
      CRITERIA criteria = this.criteriaAccessor.map(it -&gt; it.resolve(query)).orElse(null);
      SORT sort = this.sortAccessor.map(it -&gt; it.resolve(query)).orElse(null);
  
      return execute(criteria, sort, query.getOffset(), query.getRows(), keyspace, type);
  }
  </code></pre>
  
  <p>Is this behavior expected? Can Redis only page results (this works, I tested it) but not sort them (in this case, paging is totally useless)?</p>