436

I am using spring data and my DAO looks like

public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
    public findAllOrderByIdAsc();   // I want to use some thing like this
}

In above code, commented line shows my intent. Can spring Data provides inbuilt functionality to use such a method to find all records order by some column with ASC/DESC?

9 Answers 9

976
public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
    public List<StudentEntity> findAllByOrderByIdAsc();
}

The code above should work. I'm using something similar:

public List<Pilot> findTop10ByOrderByLevelDesc();

It returns 10 rows with the highest level.

IMPORTANT: Since I've been told that it's easy to miss the key point of this answer, here's a little clarification:

findAllByOrderByIdAsc(); // don't miss "by"
       ^
13
  • 7
    For your method signature to work as intended with Spring Data JPA, you should include the "all" keyword, like so: List<StudentEntity> findAllByOrderByIdAsc();. Adding a return type and removing the redundant public modifier is also a good idea ;) Apr 20, 2015 at 17:11
  • 2
    I agree that public is redundant but it keeps things clear in case someone else has to work on your code. You never know who will it be :P I didnt change anything else than method name in the authors code because it's not where the problem was and if someone doesn't know what should be there, hopefully they will learn something new. Besides, it's in my example below so they wouldn't have to search god knows where, but if you insist, then so be it :) Added 'all' keyword. Thanks.
    – Sikor
    Apr 21, 2015 at 8:13
  • 14
    Still don't understand why it's necessary to add an extra By in the front of OrderBy. The documentation doesn't tell about it.
    – Aritz
    Feb 2, 2016 at 7:58
  • 7
    @XtremeBiker From the documentation link you provided: "However, the first By acts as delimiter to indicate the start of the actual criteria." Moreover, if you scroll down to section "3.4.5. Limiting query results", there is actually an example like this, but it is not explained.
    – Sikor
    Feb 2, 2016 at 16:39
  • 2
    @Eduardo If you want to order by two or more columns, just state another column after asc/desc keyword like this: findAllByOrderByFirstcolAscSecondcolDesc (not tested, but should work). However, if method name becomes too long, I suggest switching to findAll(Pageable pageRequest) and pass the Sort object inside Pageable, which is way more flexible, because you don't have to create a separate method for each use. This way client decides how to sort, how many rows are needed etc.
    – Sikor
    May 11, 2020 at 0:08
71

AFAIK, I don't think this is possible with a direct method naming query. You can however use the built in sorting mechanism, using the Sort class. The repository has a findAll(Sort) method that you can pass an instance of Sort to. For example:

import org.springframework.data.domain.Sort;

@Repository
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDAO studentDao;

    @Override
    public List<Student> findAll() {
        return studentDao.findAll(sortByIdAsc());
    }

    private Sort sortByIdAsc() {
        return new Sort(Sort.Direction.ASC, "id");
    }
} 
4
  • 1
    This is indeed possible using findAllByOrderByIdAsc() as mentioned in Sikor's answer below... @Prashant that really should be the correct answer Jun 9, 2015 at 9:13
  • Alternative way of doing the sort in the service tier if you want to keep your repositories less cluttered. Keep in mind the result size though! Mar 5, 2016 at 20:04
  • 3
    The method findAll() in the type CrudRepository<> is not applicable for the arguments (Sort) Jul 6, 2016 at 19:25
  • 5
    @ThiagoPereira you should extends JpaRepository<> if you wish to use above example. May 17, 2018 at 9:19
63

Simple way:

repository.findAll(Sort.by(Sort.Direction.DESC, "colName"));

Source: https://www.baeldung.com/spring-data-sorting

2
  • worked for me thanks Jan 9, 2022 at 3:07
  • I was looking for how to sort with parameterised order so this is the answer which helps the most even.
    – ashley
    Apr 6, 2022 at 13:56
17

Please have a look at the Spring Data JPA - Reference Documentation, section 5.3. Query Methods, especially at section 5.3.2. Query Creation, in "Table 3. Supported keywords inside method names" (links as of 2019-05-03).

I think it has exactly what you need and same query as you stated should work...

1
15

Yes you can sort using query method in Spring Data.

Ex:ascending order or descending order by using the value of the id field.

Code:

  public interface StudentDAO extends JpaRepository<StudentEntity, Integer> {
    public findAllByOrderByIdAsc();   
}

alternative solution:

    @Repository
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDAO studentDao;

    @Override
    public List<Student> findAll() {
        return studentDao.findAll(orderByIdAsc());
    }
private Sort orderByIdAsc() {
    return new Sort(Sort.Direction.ASC, "id")
                .and(new Sort(Sort.Direction.ASC, "name"));
}
}

Spring Data Sorting: Sorting

7

I try in this example to show you a complete example to personalize your OrderBy sorts

 import java.util.List;
 import org.springframework.data.domain.Page;

 import org.springframework.stereotype.Repository;
 import org.springframework.data.domain.Sort;
 /**
 * Spring Data  repository for the User entity.
 */
 @SuppressWarnings("unused")
 @Repository
 public interface UserRepository extends JpaRepository<User, Long> {
 List <User> findAllWithCustomOrderBy(Sort sort);
 }

you will use this example : A method for build dynamically a object that instance of Sort :

import org.springframework.data.domain.Sort;
public class SampleOrderBySpring{
 Sort dynamicOrderBySort = createSort();
     public static void main( String[] args )
     {
       System.out.println("default sort \"firstName\",\"name\",\"age\",\"size\" ");
       Sort defaultSort = createStaticSort();
       System.out.println(userRepository.findAllWithCustomOrderBy(defaultSort ));
        

       String[] orderBySortedArray = {"name", "firstName"};
       System.out.println("default sort ,\"name\",\"firstName\" ");
       Sort dynamicSort = createDynamicSort(orderBySortedArray );
       System.out.println(userRepository.findAllWithCustomOrderBy(dynamicSort ));
      }
      public Sort createDynamicSort(String[] arrayOrdre) {
        return  Sort.by(arrayOrdre);
        }

   public Sort createStaticSort() {
        String[] arrayOrdre  ={"firstName","name","age","size");
        return  Sort.by(arrayOrdre);
        }
}
4

for spring boot 3, your Repository should implement JpaRepository and define the sort as follow :

Sort sortBy = Sort.by(Sort.Direction.DESC, "id");

then use it :

repository.findAll(sortBy);
1

Combining all answers above, you can write reusable code with BaseEntity:

@Data
@NoArgsConstructor
@MappedSuperclass
public abstract class BaseEntity {

  @Transient
  public static final Sort SORT_BY_CREATED_AT_DESC = 
                        Sort.by(Sort.Direction.DESC, "createdAt");

  @Id
  private Long id;
  private LocalDateTime createdAt;
  private LocalDateTime updatedAt;

  @PrePersist
  void prePersist() {
    this.createdAt = LocalDateTime.now();
  }

  @PreUpdate
  void preUpdate() {
    this.updatedAt = LocalDateTime.now();
  }
}

DAO object overloads findAll method - basically, still uses findAll()

public interface StudentDAO extends CrudRepository<StudentEntity, Long> {

  Iterable<StudentEntity> findAll(Sort sort);

}

StudentEntity extends BaseEntity which contains repeatable fields (maybe you want to sort by ID, as well)

@Getter
@Setter
@FieldDefaults(level = AccessLevel.PRIVATE)
@Entity
class StudentEntity extends BaseEntity {

  String firstName;
  String surname;

}

Finally, the service and usage of SORT_BY_CREATED_AT_DESC which probably will be used not only in the StudentService.

@Service
class StudentService {

  @Autowired
  StudentDAO studentDao;

  Iterable<StudentEntity> findStudents() {
    return this.studentDao.findAll(SORT_BY_CREATED_AT_DESC);
  }
}
2
  • Very nice and clean answer!
    – sebasira
    Sep 17, 2021 at 12:52
  • StudentDAO should extends JpaRepository, and you would not need to declare the findAll(Sort sort) method.
    – Esteban
    Nov 23, 2022 at 21:59
1
Sort sortBy = Sort.by(new Sort.Order(Sort.Direction.ASC, "name").ignoreCase());

repository.findAll(sortBy);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.