/* * Return the indices that put the elements of array in sorted order * * @param array: The array to consider */ Integer[] argSort(color[] array) { IndexComparator ic = new IndexComparator(array); Integer[] indices = ic.createIndexArray(); Arrays.sort(indices, ic); return indices; } /* A Comparator class to implement argsort */ class IndexComparator implements Comparator { private final color[] array; public IndexComparator(color[] array) { this.array = array; } public Integer[] createIndexArray() { Integer [] indices = new Integer[this.array.length]; for (int i = 0; i < array.length; i++) { indices[i] = i; } return indices; } public int compare(Integer index1, Integer index2) { return (int) (brightness(this.array[index1]) - brightness(this.array[index2])); } }