|
24 | 24 | import org.bson.Document;
|
25 | 25 | import org.springframework.data.domain.Range;
|
26 | 26 | import org.springframework.data.domain.Sort;
|
| 27 | +import org.springframework.data.domain.Sort.Direction; |
27 | 28 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Filter.AsBuilder;
|
28 | 29 | import org.springframework.data.mongodb.core.aggregation.ArrayOperators.Reduce.PropertyExpression;
|
29 | 30 | import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedField;
|
@@ -336,6 +337,22 @@ public SortArray sort(Sort sort) {
|
336 | 337 | return (usesExpression() ? SortArray.sortArrayOf(expression) : SortArray.sortArray(values)).by(sort);
|
337 | 338 | }
|
338 | 339 |
|
| 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 | + |
339 | 356 | /**
|
340 | 357 | * Creates new {@link AggregationExpression} that transposes an array of input arrays so that the first element of
|
341 | 358 | * the output array would be an array containing, the first element of the first input array, the first element of
|
@@ -2059,6 +2076,16 @@ public SortArray by(Sort sort) {
|
2059 | 2076 | return new SortArray(append("sortBy", sort));
|
2060 | 2077 | }
|
2061 | 2078 |
|
| 2079 | + /** |
| 2080 | + * Set the order to put elements in. |
| 2081 | + * |
| 2082 | + * @param direction must not be {@literal null}. |
| 2083 | + * @return new instance of {@link SortArray}. |
| 2084 | + */ |
| 2085 | + public SortArray by(Direction direction) { |
| 2086 | + return new SortArray(append("sortBy", direction.isAscending() ? 1 : -1)); |
| 2087 | + } |
| 2088 | + |
2062 | 2089 | /*
|
2063 | 2090 | * (non-Javadoc)
|
2064 | 2091 | * @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()
|
|
0 commit comments