Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 1ffa287

Browse files
nathanukeychristophstrobl
authored andcommittedApr 21, 2025
Add support for sorting simple arrays by direction.
Add method to provide sorting direction to sort array aggregation. Related to: #4929 Original Pull Request: #4935 Signed-off-by: Nathan McDonald <[email protected]>
1 parent 36a9c22 commit 1ffa287

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed
 

‎spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArrayOperators.java‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.jspecify.annotations.Nullable;
2626
import org.springframework.data.domain.Range;
2727
import org.springframework.data.domain.Sort;
28+
import org.springframework.data.domain.Sort.Direction;
2829
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
2930
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
3031
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -348,6 +349,22 @@ public SortArray sort(Sort sort) {
348349
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
349350
}
350351

352+
/**
353+
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
354+
* order}.
355+
*
356+
* @return new instance of {@link SortArray}.
357+
* @since 4.0
358+
*/
359+
public SortArray sort(Direction direction) {
360+
361+
if (usesFieldRef()) {
362+
return SortArray.sortArrayOf(fieldReference).by(direction);
363+
}
364+
365+
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
366+
}
367+
351368
/**
352369
* Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
353370
* the output array would be an array containing, the first element of the first input array, the first element of
@@ -2120,10 +2137,20 @@ public SortArray byValueDescending() {
21202137
return new SortArray(append("sortBy", -1));
21212138
}
21222139

2123-
/*
2124-
* (non-Javadoc)
2125-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2140+
/**
2141+
* Set the order to put elements in.
2142+
*
2143+
* @param direction must not be {@literal null}.
2144+
* @return new instance of {@link SortArray}.
21262145
*/
2146+
public SortArray by(Direction direction) {
2147+
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
2148+
}
2149+
2150+
/*
2151+
* (non-Javadoc)
2152+
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2153+
*/
21272154
@Override
21282155
protected String getMongoMethod() {
21292156
return "$sortArray";

‎spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArrayOperatorsUnitTests.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bson.Document;
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.data.domain.Sort;
27+
import org.springframework.data.domain.Sort.Direction;
2728
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
2829
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
2930

@@ -203,4 +204,12 @@ public void sortArrayByPropertyUnchanged() {
203204
new Document("input", "$items").append("sortBy", new Document("price", 1)));
204205
assertThat(result).isEqualTo(expected);
205206
}
207+
208+
@Test // GH-4929
209+
void sortByWithDirection() {
210+
211+
assertThat(ArrayOperators.arrayOf(List.of("a", "b", "d", "c")).sort(Direction.DESC)
212+
.toDocument(Aggregation.DEFAULT_CONTEXT))
213+
.isEqualTo("{ $sortArray: { input: [\"a\", \"b\", \"d\", \"c\"], sortBy: -1 } }");
214+
}
206215
}

0 commit comments

Comments
 (0)
Please sign in to comment.