import java.util.*;

public class CompareExp{

		public static void main(String[] args){
			TwoInt[] arr = new TwoInt[4];
			arr[0] = new TwoInt(1, 2);	
			arr[1] = new TwoInt(3, 4);
			arr[2] = new TwoInt(2, 1);
			arr[3] = new TwoInt(2, 2);
			
			for(int i=0;i<arr.length;i++)
				System.out.print(arr[i]+" ");
			System.out.println();
			
			Arrays.sort(arr);
			//Arrays.sort(arr, new TwoIntComparator());
			for(int i=0;i<arr.length;i++)
				System.out.print(arr[i]+" ");
			System.out.println();
			
			/*
			int loc = Arrays.binarySearch(arr, new TwoInt(2,2));
			//int loc = Arrays.binarySearch(arr, new TwoInt(2,2), new TwoIntComparator());
			System.out.println("find [2,2] at "+loc);
			
			loc = Arrays.binarySearch(arr, new TwoInt(2,3));
			System.out.println("find [2,3] at "+loc);
			*/
			
		}
}

class TwoInt implements Comparable{
	int i;
	int j;
	
	public TwoInt(int i, int j){
		this.i=i;
		this.j=j;	
	}	
	
	public int compareTo(Object ti){ //have to Object here to implement Comparable
		int t1 = i+j;
		int t2 = ((TwoInt)ti).i+((TwoInt)ti).j;
		return(t1<t2?-1:(t1==t2?0:1));	
	}
	
	public String toString(){
		return "[i="+i+",j="+j+"]";	
	}
}


class TwoIntComparator implements Comparator{
	
	public int compare(Object o1, Object o2){
		int t1 = ((TwoInt)o1).i+((TwoInt)o1).j;
		int t2 = ((TwoInt)o2).i+((TwoInt)o2).j;	
		return(t1>t2?-1:(t1==t2?0:1));	 //!! notice: order reversed!
	}	
}