Problem:
Sorting Pairs

Write a program which print coordinates $(x_i, y_i)$ of given $n$ points on the plane by the following criteria.

first by $x$-coordinate

in case of a tie, by $y$-coordinate

Input

The input is given in the following format.

$n$
$x_0 \; y_0$
$x_1 \; y_1$
:
$x_{n-1} \; y_{n-1}$

In the first line, the number of points $n$ is given. In the following $n$ lines, coordinates of each point are given.

Output

Print coordinate of given points in order.

Constraints

$1 \leq n \leq 100,000$

$-1,000,000,000 \leq x_i, y_i \leq 1,000,000,000$

Sample Input 1

5
4 7
5 5
2 3
6 8
2 1

Sample Output 1

2 1
2 3
4 7
5 5
6 8

Solution in JAVA (judge verdict: Accepted)

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {
    static class Point implements Comparable<Point>{
    	int point, x, y;
    	Point(int x, int y) {
    		this.x=x;
    		this.y=y;
    	}
    	public int compareTo(Point p1) {
    		if(p1.x == this.x) {
                return this.y - p1.y;
            }
            return this.x - p1.x;
    	}
    }
    
    public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)){
                int n = sc.nextInt();
                ArrayList<Point> pairs=new ArrayList<Point>();
                
                for(int i=0; i<n; i++){
                    int X = sc.nextInt();
                    int Y = sc.nextInt();
                    pairs.add(new Point(X, Y));
                }//準備
                Collections.sort(pairs);
                for(int i = 0; i < n; i++) {
                    System.out.println(pairs.get(i).x+" "+pairs.get(i).y);
                }
                
        }
    }
}