Skip to content

Commit 73f92a6

Browse files
Support for sorting simple arrays (integers/strings) with SortArray (Fixes #4929) #4943 - Removed unnecessary whitespaces
- 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. - Removed unnecessary whitespaces and formatting changes to ensure clean, readable code. For more details, refer to: https://www.mongodb.com/docs/manual/reference/operator/aggregation/sortArray/ Signed-off-by: Ranzy Blessings <[email protected]>
1 parent a5272a3 commit 73f92a6

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-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
@@ -2067,5 +2067,27 @@ public SortArray by(Sort sort) {
20672067
protected String getMongoMethod() {
20682068
return "$sortArray";
20692069
}
2070+
2071+
/**
2072+
* Sort the array elements by their values in ascending order.
2073+
* Suitable for arrays of simple types (e.g., integers, strings).
2074+
*
2075+
* @return new instance of {@link SortArray}.
2076+
* @since 4.x (TBD)
2077+
*/
2078+
public SortArray byValueAscending() {
2079+
return new SortArray(append("sortBy", 1));
2080+
}
2081+
2082+
/**
2083+
* Sort the array elements by their values in descending order.
2084+
* Suitable for arrays of simple types (e.g., integers, strings).
2085+
*
2086+
* @return new instance of {@link SortArray}.
2087+
* @since 4.x (TBD)
2088+
*/
2089+
public SortArray byValueDescending() {
2090+
return new SortArray(append("sortBy", -1));
2091+
}
20702092
}
20712093
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,26 @@ void sortByWithFieldRef() {
179179
assertThat(ArrayOperators.arrayOf("team").sort(Sort.by("name")).toDocument(Aggregation.DEFAULT_CONTEXT))
180180
.isEqualTo("{ $sortArray: { input: \"$team\", sortBy: { name: 1 } } }");
181181
}
182+
183+
@Test // GH-4929
184+
public void sortArrayByValueAscending() {
185+
Document result = ArrayOperators.SortArray.sortArrayOf("numbers").byValueAscending().toDocument(Aggregation.DEFAULT_CONTEXT);
186+
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", 1));
187+
assertThat(result).isEqualTo(expected);
188+
}
189+
190+
@Test // GH-4929
191+
public void sortArrayByValueDescending() {
192+
Document result = ArrayOperators.SortArray.sortArrayOf("numbers").byValueDescending().toDocument(Aggregation.DEFAULT_CONTEXT);
193+
Document expected = new Document("$sortArray", new Document("input", "$numbers").append("sortBy", -1));
194+
assertThat(result).isEqualTo(expected);
195+
}
196+
197+
@Test // GH-4929
198+
public void sortArrayByPropertyUnchanged() {
199+
Document result = ArrayOperators.SortArray.sortArrayOf("items")
200+
.by(Sort.by(Sort.Direction.ASC, "price")).toDocument(Aggregation.DEFAULT_CONTEXT);
201+
Document expected = new Document("$sortArray", new Document("input", "$items").append("sortBy", new Document("price", 1)));
202+
assertThat(result).isEqualTo(expected);
203+
}
182204
}

0 commit comments

Comments
 (0)