+
+ /**
+ * Sorts arrays of int and Object, by making an array of indices, and sorting
+ * it using a comparator that refers to the int values.
+ *
+ * @see http
+ * ://stackoverflow.com/questions/4859261/get-the-indices-of-an-array-
+ * after-sorting
+ * @param arr
+ * @param s
+ * @param ascending
+ */
+ public static void sortByInt(int[] arr, Object[] s, boolean ascending)
+ {
+ final int length = arr.length;
+ Integer[] indices = makeIndexArray(length);
+ Arrays.sort(indices, new IntComparator(arr, ascending));
+
+ /*
+ * Copy the array values as per the sorted indices
+ */
+ int[] sortedInts = new int[length];
+ Object[] sortedObjects = new Object[s.length];
+ for (int i = 0; i < length; i++)
+ {
+ sortedInts[i] = arr[indices[i]];
+ sortedObjects[i] = s[indices[i]];
+ }
+
+ /*
+ * And copy the sorted values back into the arrays
+ */
+ System.arraycopy(sortedInts, 0, arr, 0, length);
+ System.arraycopy(sortedObjects, 0, s, 0, s.length);
+ }
+
+ /**
+ * Sorts arrays of String and Object, by making an array of indices, and
+ * sorting it using a comparator that refers to the String values. Both arrays
+ * are sorted by case-sensitive order of the string array values.
+ *
+ * @see http
+ * ://stackoverflow.com/questions/4859261/get-the-indices-of-an-array-
+ * after-sorting
+ * @param arr
+ * @param s
+ * @param ascending
+ */
+ public static void sortByString(String[] arr, Object[] s,
+ boolean ascending)
+ {
+ final int length = arr.length;
+ Integer[] indices = makeIndexArray(length);
+ Arrays.sort(indices, new ExternalComparator(arr, ascending));
+
+ /*
+ * Copy the array values as per the sorted indices
+ */
+ String[] sortedStrings = new String[length];
+ Object[] sortedObjects = new Object[s.length];
+ for (int i = 0; i < length; i++)
+ {
+ sortedStrings[i] = arr[indices[i]];
+ sortedObjects[i] = s[indices[i]];
+ }
+
+ /*
+ * And copy the sorted values back into the arrays
+ */
+ System.arraycopy(sortedStrings, 0, arr, 0, length);
+ System.arraycopy(sortedObjects, 0, s, 0, s.length);
+ }
+
+ /**
+ * Sorts arrays of double and Object, by making an array of indices, and
+ * sorting it using a comparator that refers to the double values.
+ *
+ * @see http
+ * ://stackoverflow.com/questions/4859261/get-the-indices-of-an-array-
+ * after-sorting
+ * @param arr
+ * @param s
+ * @param ascending
+ */
+ public static void sortByDouble(double[] arr, Object[] s,
+ boolean ascending)
+ {
+ final int length = arr.length;
+ Integer[] indices = makeIndexArray(length);
+ Arrays.sort(indices, new DoubleComparator(arr, ascending));
+
+ /*
+ * Copy the array values as per the sorted indices
+ */
+ double[] sortedDoubles = new double[length];
+ Object[] sortedObjects = new Object[s.length];
+ for (int i = 0; i < length; i++)
+ {
+ sortedDoubles[i] = arr[indices[i]];
+ sortedObjects[i] = s[indices[i]];
+ }
+
+ /*
+ * And copy the sorted values back into the arrays
+ */
+ System.arraycopy(sortedDoubles, 0, arr, 0, length);
+ System.arraycopy(sortedObjects, 0, s, 0, s.length);
+ }