I have an array, not an arrayList and I would like to sort it. This only tells me that Collections are not applicable for the arguments (pixelPlaceSort.Pixel[], ...etc.
Collections.sort(pixels, new PixelComparator());
I could use a List to solve it but for the purpose of learning I don't want that.
So how can this work? c is an int.
class PixelComparator implements Comparator<Pixel> {
PixelComparator() {
}
public int compare(Pixel p1, Pixel p2) {
if(p1.c < p2.c) {
return -1;
} else if(p1.c > p2.c) {
return 1;
}
else {
return 0;
}
}
}