How to use OrderBy with findAll in Spring Data

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"
       ^

Leave a Comment