Create an Integer[] idx the same length and fill it with numbers from 0 to 999 (or something else), then sort this array using a comparator that does
public int compare(Integer a, Integer b) { return posYArray[a] - posYArray[b]; }
This will give you an array of indices in other arrays, i.e. the smallest value of Y will be posYArray[idx[0]] , and its corresponding X will be posXArray[idx[0]] , etc. If you don't want to maintain indirection, you can reorder the original arrays with idx values.
If you regularly do such things, you can look at fastutil , which provides Collection and Comparator types that work directly on primitive types such as int , avoiding the need to insert integers into the box and Unbox.
source share