Skip to content

Commit 98f871b

Browse files
Ranzy Blessingschristophstrobl
Ranzy Blessings
authored andcommitted
Add support for sorting simple arrays (integers/strings) with SortArray ()
- Added methods `byValueAscending()` and `byValueDescending()` to the SortArray class to support sorting simple array types (e.g., integers, strings) in ascending and descending order. - Updated tests to verify the correct functionality of sorting arrays by value. - Refactored SortArray to handle sorting of simple types without requiring a property for sorting. For more details, refer to: https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortArray/ Resolves: #4929 Original Pull Request: #4943 Signed-off-by: Ranzy Blessings <[email protected]>
1 parent 4bd5a1f commit 98f871b

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

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

+22
Original file line numberDiff line numberDiff line change
@@ -2059,6 +2059,28 @@ public SortArray by(Sort sort) {
20592059
return new SortArray(append("sortBy", sort));
20602060
}
20612061

2062+
/**
2063+
* Sort the array elements by their values in ascending order. Suitable for arrays of simple types (e.g., integers,
2064+
* strings).
2065+
*
2066+
* @return new instance of {@link SortArray}.
2067+
* @since 4.x (TBD)
2068+
*/
2069+
public SortArray byValueAscending() {
2070+
return new SortArray(append("sortBy", 1));
2071+
}
2072+
2073+
/**
2074+
* Sort the array elements by their values in descending order. Suitable for arrays of simple types (e.g., integers,
2075+
* strings).
2076+
*
2077+
* @return new instance of {@link SortArray}.
2078+
* @since 4.x (TBD)
2079+
*/
2080+
public SortArray byValueDescending() {
2081+
return new SortArray(append("sortBy", -1));
2082+
}
2083+
20622084
/*
20632085
* (non-Javadoc)
20642086
* @see org.springframework.data.mongodb.core.aggregation.AbstractAggregationExpression#getMongoMethod()

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

+24
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.jupiter.api.Test;
2626
import org.springframework.data.domain.Sort;
2727
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.ArrayToObject;
28+
import org.springframework.data.mongodb.core.aggregation.ArrayOperators.SortArray;
2829

2930
/**
3031
* Unit tests for {@link ArrayOperators}
@@ -179,4 +180,27 @@ void sortByWithFieldRef() {
179180
assertThat(ArrayOperators.arrayOf("team").sort(Sort.by("name")).toDocument(Aggregation.DEFAULT_CONTEXT))
180181
.isEqualTo("{ $sortArray: { input: \"$team\", sortBy: { name: 1 } } }");
181182
}
183+
184+
@Test // GH-4929
185+
public void sortArrayByValueAscending() {
186+
Document result = SortArray.sortArrayOf("numbers").byValueAscending().toDocument(Aggregation.DEFAULT_CONTEXT);
187+
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", 1));
188+
assertThat(result).isEqualTo(expected);
189+
}
190+
191+
@Test // GH-4929
192+
public void sortArrayByValueDescending() {
193+
Document result = SortArray.sortArrayOf("numbers").byValueDescending().toDocument(Aggregation.DEFAULT_CONTEXT);
194+
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", -1));
195+
assertThat(result).isEqualTo(expected);
196+
}
197+
198+
@Test // GH-4929
199+
public void sortArrayByPropertyUnchanged() {
200+
Document result = SortArray.sortArrayOf("items").by(Sort.by(Sort.Direction.ASC, "price"))
201+
.toDocument(Aggregation.DEFAULT_CONTEXT);
202+
Document expected = new Document("$sortArray",
203+
new Document("input", "$items").append("sortBy", new Document("price", 1)));
204+
assertThat(result).isEqualTo(expected);
205+
}
182206
}

0 commit comments

Comments
 (0)