How to keep the sorting order while paging in Spring
<p>I have a spring application which utilize Spring MVC and Spring data. I am trying to enable paging and sorting in a view by using Pageable and Sort argument in my controller methods. Everything works fine for first page when I sort the results by title or date, but when I navigate to next page results are not ordered anymore my <code>mvcContext.xml</code> file is </p>

<pre><code>&lt;beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"&gt;

    &lt;mvc:annotation-driven&gt;
        &lt;mvc:argument-resolvers&gt;
            &lt;bean class="org.springframework.data.web.PageableHandlerMethodArgumentResolver"&gt;
                &lt;property name="maxPageSize" value="3"/&gt;
            &lt;/bean&gt;
            &lt;bean class="org.springframework.data.web.SortHandlerMethodArgumentResolver"/&gt;
        &lt;/mvc:argument-resolvers&gt;
    &lt;/mvc:annotation-driven&gt;
    &lt;context:component-scan base-package="com.its.stud"/&gt;
    &lt;mvc:resources mapping="/resources/**" location="/resources/"/&gt;

    &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix" value="/WEB-INF/views/"/&gt;
        &lt;property name="suffix" value=".jsp"/&gt;
    &lt;/bean&gt;
&lt;/beans&gt;
</code></pre>

<p>and my jsp file is</p>

<pre><code> &lt;c:forEach items="${page.content}" var="topic"&gt;
    &lt;div class="topic-box"&gt;
        &lt;div class="from-topicbov"  &gt;
            &lt;div class="form-topic-title"&gt;
                &lt;h1&lt;a href="&lt;spring:url value="/logged?page=0&amp;sort=title"/&gt;"&gt;Title&lt;/a&gt;&lt;/h1&gt;
                &lt;p&gt;&lt;a href="&lt;spring:url value="/logged?page=0&amp;sort=date"/&gt;"&gt;Date&lt;/a&gt;&lt;/p&gt;
            &lt;/div&gt;
            &lt;div class="form-row"&gt;
                &lt;label&gt;
                    &lt;p &gt;${topic.content}&lt;/p&gt;
                    &lt;div class="date"&gt;
                        &lt;p&gt;&lt;a href="&lt;spring:url value="/logged/${topic.id}"/&gt;"&gt;${topic.title}&lt;/a&gt;&lt;/p&gt;
                        &lt;p&gt;${topic.date}&lt;/p&gt;
                        &lt;p&gt;${topic.author}&lt;/p&gt;
                    &lt;/div&gt;
                &lt;/label&gt;
            &lt;/div&gt;
            &lt;div class="form-row"&gt;
                &lt;button type="submit" &gt;comment&lt;/button&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
    &lt;/c:forEach&gt;
</code></pre>

<p>and in my controller class I use this method:</p>

<pre><code>  @RequestMapping("/logged")
    public String welcome(Model model, Pageable page, Sort sort) {
        model.addAttribute("page",topicRepository.findAll(page));
        return "topics";
    }
</code></pre>

<p>Please advice.
EDIT: the repository </p>

<pre><code>public interface TopicRepository extends JpaRepository&lt;Topic,Long&gt; {
}
</code></pre>