Skip to content

Commit 46ff9d2

Browse files
committed
Allow reversing Sort.
We now allow reversing the sort order of Sort instances to flip ASC and DESC sort orders. Closes #2805
1 parent 1ba2932 commit 46ff9d2

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/main/java/org/springframework/data/domain/Sort.java

+44-3
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public Sort and(Sort sort) {
194194

195195
Assert.notNull(sort, "Sort must not be null");
196196

197-
ArrayList<Order> these = new ArrayList<Order>(this.toList());
197+
List<Order> these = new ArrayList<Order>(this.toList());
198198

199199
for (Order order : sort) {
200200
these.add(order);
@@ -203,6 +203,31 @@ public Sort and(Sort sort) {
203203
return Sort.by(these);
204204
}
205205

206+
/**
207+
* Returns a new {@link Sort} with reversed sort {@link Order}s turning effectively asccending into descending sort
208+
* order and vice versa.
209+
*
210+
* @return a new {@link Sort} object with reversed sort orders applied.
211+
* @since 3.1
212+
*/
213+
public Sort reverse() {
214+
215+
List<Order> reversed = doReverse();
216+
217+
return Sort.by(reversed);
218+
}
219+
220+
protected List<Order> doReverse() {
221+
222+
List<Order> reversed = new ArrayList<>(orders.size());
223+
224+
for (Order order : this) {
225+
reversed.add(order.reverse());
226+
}
227+
228+
return reversed;
229+
}
230+
206231
/**
207232
* Returns the order registered for the given property.
208233
*
@@ -260,7 +285,13 @@ public String toString() {
260285
*/
261286
private Sort withDirection(Direction direction) {
262287

263-
return Sort.by(stream().map(it -> it.with(direction)).collect(Collectors.toList()));
288+
List<Order> result = new ArrayList<>(orders.size());
289+
290+
for (Order order : this) {
291+
result.add(order.with(direction));
292+
}
293+
294+
return Sort.by(result);
264295
}
265296

266297
/**
@@ -332,7 +363,7 @@ public static Optional<Direction> fromOptionalString(String value) {
332363
* @author Thomas Darimont
333364
* @since 1.8
334365
*/
335-
public static enum NullHandling {
366+
public enum NullHandling {
336367

337368
/**
338369
* Lets the data store decide what to do with nulls.
@@ -503,6 +534,16 @@ public Order with(Direction direction) {
503534
return new Order(direction, this.property, this.ignoreCase, this.nullHandling);
504535
}
505536

537+
/**
538+
* Returns a new {@link Order} with the reversed {@link #getDirection()}.
539+
*
540+
* @return
541+
* @since 3.1
542+
*/
543+
public Order reverse() {
544+
return with(this.direction == Direction.ASC ? Direction.DESC : Direction.ASC);
545+
}
546+
506547
/**
507548
* Returns a new {@link Order}
508549
*

src/test/java/org/springframework/data/domain/SortUnitTests.java

+15
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ void allowsCombiningTypedSorts() {
201201
.containsExactly(Order.by("center"), Order.by("radius"));
202202
}
203203

204+
@Test // GH-2805
205+
void reversesSortCorrectly() {
206+
assertThat(Sort.by(Order.asc("center"), Order.desc("radius")).reverse()) //
207+
.containsExactly(Order.desc("center"), Order.asc("radius"));
208+
}
209+
210+
@Test // GH-2805
211+
void reversesTypedSortCorrectly() {
212+
213+
Sort reverse = Sort.sort(Circle.class).by(Circle::getCenter).reverse();
214+
assertThat(reverse) //
215+
.containsExactly(Order.desc("center"));
216+
217+
}
218+
204219
@Getter
205220
static class Sample {
206221
Nested nested;

0 commit comments

Comments
 (0)