How to sort columns multiple columns in spring jpa which has parameters being passed?

Viewed 20

I have a Jpa repository and I need to sort by two columns i order.

public interface PetRepository extends JpaRepository<PetClass, PetKey> {
    List<PetClass> findAllByStatusAndWeightAndBirthDateBetween(String status, String weight, Date fromDate, Date toDate);
}

First I need to sort it by PetClass's petHeight property in ascending order and then by petLength property in descending order. If I needed to sort only one column then I could have done findAllByStatusAndWeightAndBirthDateBetweenOrderByPetLength but I can't use two 'orderBy' in one method it seems because it throws an error saying such.

1 Answers

You can easily construct multi sort in repository

return this.PetRepository.findAll(Sort.by("status").descending().and(Sort.by("weight")).a cending()); 
Related