@@ -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 *
0 commit comments