알고리즘 풀이/백준

[백준][Java] 11650번 좌표 정렬하기

배게 2018. 5. 16. 23:48
728x90

Arrays.sort를 통해 2차원 배열을 정렬하는 방법을


저번에 한번 했던 것 같은데 까먹었었다.


메소드 사용방법만 알면 개껌인 문제이다.


메소드가 어떤 원리인지 공부하도록해야겟다.



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		
		Scanner sc = new Scanner (System.in);
		
		int N = sc.nextInt();
		int[][] xy = new int[N][2];
		
		for(int i=0; i<N; i++) {
			xy[i][0]=sc.nextInt();
			xy[i][1]=sc.nextInt();
		}
		
		Arrays.sort(xy, new Comparator<int []>() {
			@Override
			public int compare(int[] o1, int[] o2) {
				// TODO Auto-generated method stub
				if(o1[0]==o2[0]) return Integer.compare(o1[1], o2[1]);
				return Integer.compare(o1[0], o2[0]);
			}
		});
		
		for(int i=0; i<N; i++) {
			System.out.println(xy[i][0]+" "+xy[i][1]);
		}
		
	}
}