@@ -194,7 +194,7 @@ public Sort and(Sort sort) {
194
194
195
195
Assert .notNull (sort , "Sort must not be null" );
196
196
197
- ArrayList <Order > these = new ArrayList <Order >(this .toList ());
197
+ List <Order > these = new ArrayList <Order >(this .toList ());
198
198
199
199
for (Order order : sort ) {
200
200
these .add (order );
@@ -203,6 +203,31 @@ public Sort and(Sort sort) {
203
203
return Sort .by (these );
204
204
}
205
205
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
+
206
231
/**
207
232
* Returns the order registered for the given property.
208
233
*
@@ -260,7 +285,13 @@ public String toString() {
260
285
*/
261
286
private Sort withDirection (Direction direction ) {
262
287
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 );
264
295
}
265
296
266
297
/**
@@ -332,7 +363,7 @@ public static Optional<Direction> fromOptionalString(String value) {
332
363
* @author Thomas Darimont
333
364
* @since 1.8
334
365
*/
335
- public static enum NullHandling {
366
+ public enum NullHandling {
336
367
337
368
/**
338
369
* Lets the data store decide what to do with nulls.
@@ -503,6 +534,16 @@ public Order with(Direction direction) {
503
534
return new Order (direction , this .property , this .ignoreCase , this .nullHandling );
504
535
}
505
536
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
+
506
547
/**
507
548
* Returns a new {@link Order}
508
549
*
0 commit comments