Java Utililty Methods Array Sort

List of utility methods to do Array Sort

Description

The list of methods to do Array Sort are organized into topic(s).

Method

voidsortDesc(long[] keys, int[] values)
Sorts the keys in an decreasing order.
hybridsortDesc(keys, values, null, null, 0, keys.length - 1);
T[]sortDescending(T[] a, int fromIndex, int toIndex)
sort Descending
Arrays.sort(a, fromIndex, toIndex);
toIndex--;
while (fromIndex < toIndex) {
    T t = a[fromIndex];
    a[fromIndex++] = a[toIndex];
    a[toIndex--] = t;
return a;
...
voidSortDoubleDimensionArray(String StrArr[][])
Sort Double Dimension Array
if (StrArr != null) {
    Arrays.sort(StrArr, new Comparator<String[]>() {
        @Override
        public int compare(final String[] entry1, final String[] entry2) {
            final String val1 = entry1[0];
            final String val2 = entry2[0];
            return val1.compareTo(val2);
    });
booleansorted(int[] array)
sorted
int last = array[0], current;
for (int i = 0; i < array.length; i++) {
    current = array[i];
    if (last > current) {
        return false;
    last = current;
return true;
booleansortedArraysEqual(Object[] arr1, Object[] arr2)
Determine whether two arrays contain the same elements, even if those elements aren't in the same order in both arrays.
if (arr1 == null) {
    return (arr2 == null);
if (arr1.getClass() != arr2.getClass()) {
    return false;
if (arr1.length != arr2.length) {
    return false;
...
int[]sortedCopyOf(final int[] array)
sorted Copy Of
final int[] copyOfArray = array.clone();
Arrays.sort(copyOfArray);
return copyOfArray;
int[]sortedIndex(double[] values)
Sort an array and return the result as an array of indices
class indexValuePair {
    int index;
    double value;
    public indexValuePair(int index, double value) {
        this.index = index;
        this.value = value;
class ValueComparator implements java.util.Comparator<indexValuePair> {
    public int compare(indexValuePair o1, indexValuePair o2) {
        return new Double(((indexValuePair) o1).value).compareTo(new Double(((indexValuePair) o2).value));
int[] result = new int[values.length];
java.util.ArrayList<indexValuePair> sortedValues = new java.util.ArrayList<indexValuePair>();
for (int i = 0; i < values.length; i++) {
    sortedValues.add(new indexValuePair(i, values[i]));
ValueComparator vc = new ValueComparator();
java.util.Collections.sort(sortedValues, vc);
int j = 0;
for (indexValuePair ivp : sortedValues) {
    result[j] = ivp.index;
    j++;
return result;
intsortedIndexOf(Comparable[] list, Comparable val, int lower, int higher)
Quickly finds the index of the given object in the list.
if (lower >= higher) {
    if (val.compareTo(list[lower]) > 0) {
        return lower + 1;
    } else {
        return lower;
int mid = (lower + higher) / 2;
...
int[]sortedMerge(int[] a, int[] b)
sorted Merge
int[] res = new int[a.length + b.length];
int i = 0, j = 0;
for (int k = 0; k < res.length; ++k) {
    if (i == a.length) {
        System.arraycopy(b, j, res, k, res.length - k);
        j = b.length;
        break;
    if (j == b.length) {
        System.arraycopy(a, i, res, k, res.length - k);
        i = a.length;
        break;
    res[k] = (a[i] > b[j]) ? b[j++] : a[i++];
assert i == a.length && j == b.length;
return res;
voidsortedMerge(int[] aIds, double[] aVals, int[] bIds, double[] bVals, int[] resIds, double[] resVals)
sorted Merge
int i = 0, j = 0;
for (int k = 0; k < resIds.length; ++k) {
    if (i == aIds.length) {
        System.arraycopy(bIds, j, resIds, k, resIds.length - k);
        System.arraycopy(bVals, j, resVals, k, resVals.length - k);
        j = bIds.length;
        break;
    if (j == bIds.length) {
        System.arraycopy(aIds, i, resIds, k, resIds.length - k);
        System.arraycopy(aVals, i, resVals, k, resVals.length - k);
        i = aIds.length;
        break;
    if (aIds[i] > bIds[j]) {
        resIds[k] = bIds[j];
        resVals[k] = bVals[j];
        ++j;
    } else {
        resIds[k] = aIds[i];
        resVals[k] = aVals[i];
        ++i;
assert i == aIds.length && j == bIds.length;