diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 9d070a50b..ef11ac6b9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -59,7 +59,6 @@ The type attribute can be add,update,fix,remove. ArrayUtils.contains() and indexOf() fails to handle Double.NaN #647. Fix potential NPE in TypeUtils.isAssignable(Type, ParameterizedType, Map, Type>). TypeUtils.isAssignable returns wrong result for GenericArrayType and ParameterizedType, #643. - Add BooleanUtils.booleanValues(). Add BooleanUtils.primitiveValues(). @@ -68,6 +67,7 @@ The type attribute can be add,update,fix,remove. More test coverage for CharSequenceUtils. #631. ArrayUtils.toPrimitive(Object) does not support boolean and other types #607. Add fluent-style ArrayUtils.sort(Object[]). + Add fluent-style ArrayUtils.sort(Object[], Comparable). Enable Dependabot #587. Bump junit-jupiter from 5.6.2 to 5.7.0. diff --git a/src/main/java/org/apache/commons/lang3/ArrayUtils.java b/src/main/java/org/apache/commons/lang3/ArrayUtils.java index ccc96987b..cee5f3d61 100644 --- a/src/main/java/org/apache/commons/lang3/ArrayUtils.java +++ b/src/main/java/org/apache/commons/lang3/ArrayUtils.java @@ -7872,6 +7872,22 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd return array; } + /** + * Sorts and returns the given array. + * + * @param the array type. + * @param array the array to sort. + * @param comparator the comparator to determine the order of the array. + * A {@code null} value uses the elements' {@link Comparable natural ordering}. + * @return the given array. + * @see Arrays#sort(Object[]) + * @since 3.12 + */ + public static T[] sort(T[] array, Comparator comparator) { + Arrays.sort(array, comparator); + return array; + } + /** *

Produces a new {@code boolean} array containing the elements * between the start and end indices. diff --git a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java index 5b5f88925..99ea44d7b 100644 --- a/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ArrayUtilsTest.java @@ -5120,6 +5120,13 @@ public class ArrayUtilsTest { assertEquals(array1, ArrayUtils.sort(array2)); } + public void testSortComparable() { + final String[] array1 = ArrayUtils.toArray("foo", "bar"); + final String[] array2 = array1.clone(); + Arrays.sort(array1); + assertEquals(array1, ArrayUtils.sort(array2, String::compareTo)); + } + @Test public void testSubarrayBoolean() { final boolean[] nullArray = null; diff --git a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java index 8cdce0b89..a3711bc53 100644 --- a/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/reflect/TypeUtilsTest.java @@ -790,7 +790,7 @@ public class TypeUtilsTest { assertFalse(paramType.getClass().isAssignableFrom(WildcardType.class)); WildcardType testType = TypeUtils.WILDCARD_ALL; - // TODO This test returns true unlike the test above. + // TODO This test returns true unlike the test above. // Is this a bug in this test or in the main code? assertFalse(TypeUtils.isAssignable(paramType, testType), () -> String.format("TypeUtils.isAssignable(%s, %s)", paramType, testType));