https://www.acmicpc.net/problem/11650
n개 static class Pos implements Comparable<Pos> {
int x, y;
Pos (int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pos o) {
if (this.x == o.x) return this.y - o.y; // x좌표가 같다면 y좌표 오름차순
return this.x - o.x; // x좌표 오름차순 정렬
}
}
n = Integer.parseInt(br.readLine());
pos = new Pos[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
pos[i] = new Pos(x, y); // 좌푯값 저장
}
Arrays.sort(pos); // 정렬
Arrays.sort() 메서드를 사용하면 위에 오버라이딩한 코드를 기준으로 오버라이딩 됩니다.import java.util.*;
import java.io.*;
public class Main {
static class Pos implements Comparable<Pos> {
int x, y;
Pos (int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Pos o) {
if (this.x == o.x) return this.y - o.y;
return this.x - o.x;
}
}
static int n;
static Pos[] pos;
static StringBuilder sb = new StringBuilder();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
n = Integer.parseInt(br.readLine());
pos = new Pos[n];
for (int i = 0; i < n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
pos[i] = new Pos(x, y);
}
Arrays.sort(pos);
for (Pos p : pos) sb.append(p.x + " " + p.y + "\n");
System.out.println(sb.toString());
}
}