Use ArraySorter.

This commit is contained in:
Gary Gregory 2020-12-22 16:06:55 -05:00
parent 5aab41b365
commit 44b0bb5273
4 changed files with 8 additions and 11 deletions

View File

@ -5231,8 +5231,7 @@ public static int indexOf(final int[] array, final int valueToFind, int startInd
static Object removeAll(final Object array, final int... indices) { static Object removeAll(final Object array, final int... indices) {
final int length = getLength(array); final int length = getLength(array);
int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed int diff = 0; // number of distinct indexes, i.e. number of entries that will be removed
final int[] clonedIndices = clone(indices); final int[] clonedIndices = ArraySorter.sort(clone(indices));
Arrays.sort(clonedIndices);
// identify length of result array // identify length of result array
if (isNotEmpty(clonedIndices)) { if (isNotEmpty(clonedIndices)) {

View File

@ -20,12 +20,12 @@ package org.apache.commons.lang3.builder;
import java.lang.reflect.AccessibleObject; import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
@ -191,8 +191,7 @@ public class HashCodeBuilder implements Builder<Integer> {
try { try {
register(object); register(object);
// The elements in the returned array are not sorted and are not in any particular order. // The elements in the returned array are not sorted and are not in any particular order.
final Field[] fields = clazz.getDeclaredFields(); final Field[] fields = ArraySorter.sort(clazz.getDeclaredFields(), Comparator.comparing(Field::getName));
Arrays.sort(fields, Comparator.comparing(Field::getName));
AccessibleObject.setAccessible(fields, true); AccessibleObject.setAccessible(fields, true);
for (final Field field : fields) { for (final Field field : fields) {
if (!ArrayUtils.contains(excludeFields, field.getName()) if (!ArrayUtils.contains(excludeFields, field.getName())

View File

@ -26,6 +26,7 @@ import java.util.Collection;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.ClassUtils;
import org.apache.commons.lang3.Validate; import org.apache.commons.lang3.Validate;
@ -640,8 +641,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
return; return;
} }
// The elements in the returned array are not sorted and are not in any particular order. // The elements in the returned array are not sorted and are not in any particular order.
final Field[] fields = clazz.getDeclaredFields(); final Field[] fields = ArraySorter.sort(clazz.getDeclaredFields(), Comparator.comparing(Field::getName));
Arrays.sort(fields, Comparator.comparing(Field::getName));
AccessibleObject.setAccessible(fields, true); AccessibleObject.setAccessible(fields, true);
for (final Field field : fields) { for (final Field field : fields) {
final String fieldName = field.getName(); final String fieldName = field.getName();
@ -801,8 +801,7 @@ public class ReflectionToStringBuilder extends ToStringBuilder {
this.excludeFieldNames = null; this.excludeFieldNames = null;
} else { } else {
//clone and remove nulls //clone and remove nulls
this.excludeFieldNames = toNoNullStringArray(excludeFieldNamesParam); this.excludeFieldNames = ArraySorter.sort(toNoNullStringArray(excludeFieldNamesParam));
Arrays.sort(this.excludeFieldNames);
} }
return this; return this;
} }

View File

@ -18,6 +18,7 @@ package org.apache.commons.lang3.text;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.lang3.ArraySorter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
/** /**
@ -284,8 +285,7 @@ public abstract class StrMatcher {
* @param chars the characters to match, must not be null * @param chars the characters to match, must not be null
*/ */
CharSetMatcher(final char[] chars) { CharSetMatcher(final char[] chars) {
this.chars = chars.clone(); this.chars = ArraySorter.sort(chars.clone());
Arrays.sort(this.chars);
} }
/** /**