728x90
조건
1. x좌표를 증가하는 순으로 정렬
2. x좌표가 같을 경우 y좌표가 증가하는 순으로 정렬
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N;
N = scan.nextInt();
int[][] array = new int[N][2];
for(int i = 0; i < N; i++) {
array[i][0] = scan.nextInt();
array[i][1] = scan.nextInt();
}
Arrays.sort(array, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0])
return o1[1] - o2[1];
else
return o1[0] - o2[0];
}
});
for(int i = 0; i < N; i++) {
System.out.println(array[i][0] + " " + array[i][1]);
}
scan.close();
}
}
https://st-lab.tistory.com/243 를 참고해서 comparable이랑 compareTo 공부하자!
728x90
'CS > Algorithm' 카테고리의 다른 글
[ JAVA / 백준 : 1427 ] 소트인사이드 (0) | 2021.08.08 |
---|---|
[ JAVA / 백준 : 10814 ] 나이순 정렬 (0) | 2021.08.08 |
[ JAVA / 백준 : 1316 ] 그룹단어체커 (0) | 2021.08.02 |
[ JAVA / 백준 : 2615 ] 오목 (0) | 2021.07.31 |
[ JAVA / 백준 : 11723] 집합 (0) | 2021.07.30 |