xenoterracide
xenoterracide

Reputation: 16837

How can I sort one column by ascending and another by descending in Spring Data?

Given the following page request let's say I wanted to Sort Descending by "created" but ascending by "name", how would I do that? the api doesn't seem to allow "direction" "field" pairs.

new PageRequest( 1, 15, Sort.Direction.DESC, "created", "name" )

using Spring Data JPA 1.6.5.

Upvotes: 3

Views: 5769

Answers (2)

Santosh Ghodekar
Santosh Ghodekar

Reputation: 1

new PageRequest(1, 15, Sort
         .by(Sort.Direction.DESC, "created")
         .and(Sort.Direction.ASC, "name"))

Upvotes: 0

Predrag Maric
Predrag Maric

Reputation: 24403

Try this

new PageRequest(1, 15, new Sort(
    new Order(Direction.DESC, "created"), 
    new Order(Direction.ASC, "name")
  )

Upvotes: 2

Related Questions