in commons-numbers-arrays/src/main/java/org/apache/commons/numbers/arrays/SortInPlace.java [66:107]
public void apply(double[] x,
double[]... yList) {
final int yListLen = yList.length;
final int len = x.length;
for (int j = 0; j < yListLen; j++) {
final double[] y = yList[j];
if (y.length != len) {
throw new IllegalArgumentException("Size mismatch: " +
y.length + " != " + len);
}
}
// Associate each abscissa "x[i]" with its index "i".
final List<PairDoubleInteger> list = new ArrayList<>(len);
for (int i = 0; i < len; i++) {
list.add(new PairDoubleInteger(x[i], i));
}
// Sort.
Collections.sort(list, comparator);
// Modify the original array so that its elements are in the prescribed order.
// Retrieve indices of original locations.
final int[] indices = new int[len];
for (int i = 0; i < len; i++) {
final PairDoubleInteger e = list.get(i);
x[i] = e.key();
indices[i] = e.value();
}
// In every associated array, move the elements to their new location.
for (int j = 0; j < yListLen; j++) {
// Input array will be modified in place.
final double[] yInPlace = yList[j];
final double[] yOrig = Arrays.copyOf(yInPlace, len);
for (int i = 0; i < len; i++) {
yInPlace[i] = yOrig[indices[i]];
}
}
}