ABpositive
ABpositive

Reputation: 303

Springboot find items by title then sort order by date

I have this query in my repo for the object. The request compiles and executes fine, it even returns results. Just not in the desired order.

The intended order is Newest date -> Oldest date

What is returned is order Lowest ID -> Highest ID

The object is as follows:

public class Post {
    @Id
    @GeneratedValue( strategy = GenerationType.IDENTITY)
    private Long id;
    private String title;
    private String summary;
    private String content;
    private String author;
    private LocalDate creation;
    private String video;

Repo request:

List<Post> findPostByTitleContainsOrderByCreationDesc(String title);

Upvotes: 0

Views: 142

Answers (1)

Eyasu Tewodros
Eyasu Tewodros

Reputation: 275

The problem is type of date private LocalDate creation;

Use the following code :

@Query("SELECT p FROM Post p WHERE p.title=:title 
        ORDER BY DATE(p.localDate) ASC, TIME(p.localTime) ASC")
List<Post> findByTitle(@Param("title") String title);

Upvotes: 1

Related Questions