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

int[][]sortByFirst(int[] array1, int[] array2)
sort By First
int[][] sorted = new int[array1.length][2];
for (int i = 0; i < array1.length; i++) {
    sorted[i][0] = array1[i];
    sorted[i][1] = array2[i];
Arrays.sort(sorted, intArray_2_Comparator);
int[][] result = new int[2][array1.length];
for (int i = 0; i < array1.length; i++) {
...
Integer[]sortByIndex(final double[] data, int size)
sort By Index
Integer[] idx = new Integer[size];
for (int i = 0; i < size; i++) {
    idx[i] = i;
Arrays.sort(idx, new Comparator<Integer>() {
    @Override
    public int compare(final Integer o1, final Integer o2) {
        return Double.compare(data[o1], data[o2]);
...
voidsortByIndex(int start, int end, int[] indexes, double[] values)
In-place sort of two arrays, only indexes is used for comparison and values of same position are sorted accordingly.
int tempIx;
double tempVal;
int length = end - start;
if (length < 7) {
    for (int i = start + 1; i < end; i++) {
        for (int j = i; j > start && indexes[j - 1] > indexes[j]; j--) {
            tempIx = indexes[j];
            indexes[j] = indexes[j - 1];
...
voidsortByIndex(int start, int end, int[] indexes, double[] values)
In-place sort of two arrays, only indexes is used for comparison and values of same position are sorted accordingly.
int tempIx;
double tempVal;
int length = end - start;
if (length < 7) {
    for (int i = start + 1; i < end; i++) {
        for (int j = i; j > start && indexes[j - 1] > indexes[j]; j--) {
            tempIx = indexes[j];
            indexes[j] = indexes[j - 1];
...
String[]sortByLength(String[] proposals)
sort By Length
Arrays.sort(proposals, new Comparator() {
    public int compare(Object o1, Object o2) {
        return ((String) o2).length() - ((String) o1).length();
});
return proposals;
voidsortByLengthDesc(String[] ss)

Sorts an array of strings by their length in descending order.

This sort is guaranteed to be stable: strings of equal length are not reordered.

Comparator<String> lengthC = new Comparator<String>() {
    public int compare(String s1, String s2) {
        return s2.length() - s1.length();
};
Arrays.sort(ss, lengthC);
voidsortByString(Object[] array)
Performs a quicksort of the given objects by their string representation in ascending order.
qSortByString(array, 0, array.length - 1);
voidsortByValue(int start, int end, double[] values, int[] indexes)
sort By Value
double tempVal;
int tempIx;
int length = end - start;
if (length < 7) {
    for (int i = start + 1; i < end; i++) {
        for (int j = i; j > start && values[j - 1] > values[j]; j--) {
            tempVal = values[j];
            values[j] = values[j - 1];
...
voidsortByValueStable(int start, int end, double[] values, int[] indexes)
In-place sort of two arrays, only indexes is used for comparison and values of same position are sorted accordingly.
sortByValue(start, end, values, indexes);
for (int i = 0; i < values.length - 1; i++) {
    double tmp = values[i];
    int len = 0;
    while (i + len + 1 < values.length && tmp == values[i + len + 1])
        len++;
    if (len > 0) {
        Arrays.sort(indexes, i, i + len + 1);
...
booleansortCompare(String[] valuesOne, String[] valuesTwo)
sort Compare
if (valuesOne == null) {
    return valuesTwo == null;
if (valuesTwo == null) {
    return false;
String[] copyOne = copySortArray(valuesOne);
String[] copyTwo = copySortArray(valuesTwo);
...