|
25 | 25 | import org.jspecify.annotations.Nullable;
|
26 | 26 | import org.springframework.data.domain.Range;
|
27 | 27 | import org.springframework.data.domain.Sort;
|
| 28 | +import org.springframework.data.domain.Sort.Direction; |
28 | 29 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
|
29 | 30 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
30 | 31 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
@@ -348,6 +349,22 @@ public SortArray sort(Sort sort) {
|
348 | 349 | return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
|
349 | 350 | }
|
350 | 351 |
|
| 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 | + |
351 | 368 | /**
|
352 | 369 | * Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
|
353 | 370 | * 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() {
|
2120 | 2137 | return new SortArray(append("sortBy", -1));
|
2121 | 2138 | }
|
2122 | 2139 |
|
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}. |
2126 | 2145 | */
|
| 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 | + */ |
2127 | 2154 | @Override
|
2128 | 2155 | protected String getMongoMethod() {
|
2129 | 2156 | return "$sortArray";
|
|
0 commit comments