So, you want to sort this 4 pixel array, and only keep the 3 unique elements, and sort them?
Pixel[] arrayPix = new Pixel[4] ;
arrayPix [0] = new Pixel(1,3) ;
arrayPix [1] = new Pixel(1,1) ;
arrayPix [2] = new Pixel(1,1) ;
arrayPix [3] = new Pixel(1,2) ;
1? use Set, to keep only unique pixels
final Set set = new HashSet () ;
set.addAll(Arrays.asList(arrayPix)) ;
2? use Arrays.sort () to sort the pixels
final Comparator c = new Comparator(){
public int compare( Object o1 , Object o2 )
{
final Pixel pixel = ( (Pixel ) o2 );
final Pixel this_ = ( (Pixel ) o1 );
final int rad1 = pixel.x + pixel.y;
final int radThis = this_.x + this_.y;
return radThis - rad1 ;
}
};
Arrays.sort(pixels, c);
Don't forget to override, in the class Pixel, equals() and hashCode(), for the Collection API to work correctly:
public class Pixel
{
public Pixel( int i_x , int i_y ) {
x = i_x;
y = i_y;
}
public int x ;
public int y ;
public boolean equals( Object obj )
{
final Pixel pixel = ( (Pixel ) obj );
return this.x == pixel.x && this.y == pixel.y;
}
public int hashCode()
{
return x + y ;
}
public String toString()
{
return "[Pixel: x=" + x + ",y="+y+ "]";
}
}