Skip to content

Commit 5db56b2

Browse files
nathanukeychristophstrobl
authored andcommitted
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 98f871b commit 5db56b2

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

Diff for: spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ArrayOperators.java

+30-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.bson.Document;
2525
import org.springframework.data.domain.Range;
2626
import org.springframework.data.domain.Sort;
27+
import org.springframework.data.domain.Sort.Direction;
2728
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
2829
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
2930
import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
@@ -336,6 +337,22 @@ public SortArray sort(Sort sort) {
336337
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
337338
}
338339

340+
/**
341+
* Creates new {@link AggregationExpression} that takes the associated array and sorts it by the given {@link Sort
342+
* order}.
343+
*
344+
* @return new instance of {@link SortArray}.
345+
* @since 4.0
346+
*/
347+
public SortArray sort(Direction direction) {
348+
349+
if (usesFieldRef()) {
350+
return SortArray.sortArrayOf(fieldReference).by(direction);
351+
}
352+
353+
return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(direction);
354+
}
355+
339356
/**
340357
* Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
341358
* the output array would be an array containing, the first element of the first input array, the first element of
@@ -2081,10 +2098,20 @@ public SortArray byValueDescending() {
20812098
return new SortArray(append("sortBy", -1));
20822099
}
20832100

2084-
/*
2085-
* (non-Javadoc)
2086-
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2101+
/**
2102+
* Set the order to put elements in.
2103+
*
2104+
* @param direction must not be {@literal null}.
2105+
* @return new instance of {@link SortArray}.
20872106
*/
2107+
public SortArray by(Direction direction) {
2108+
return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1));
2109+
}
2110+
2111+
/*
2112+
* (non-Javadoc)
2113+
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
2114+
*/
20882115
@Override
20892116
protected String getMongoMethod() {
20902117
return "$sortArray";

Diff for: spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/ArrayOperatorsUnitTests.java

+9
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)