티스토리 뷰
- Comparator인터페이스를 이용해서 compare 메소드를 오버라이딩하여 정렬했다.
- sort함수에 arr을 받고 Comparator를 생성해주면서 compare 메소드를 오버라이딩한다.
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[][] = new int[n][2];
for(int i = 0; i < n; i++) {
for(int j = 0; j < arr[i].length; j++) {
arr[i][j] = sc.nextInt();
}
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
if(o1[0] == o2[0])
return Integer.compare(o1[1], o2[1]);
return Integer.compare(o1[0], o2[0]);
}
});
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i][0]+" "+arr[i][1]);
}
}
}
'algorithm' 카테고리의 다른 글
[Algorithm] LeetCode 202.happy number using HashSet (0) | 2020.05.18 |
---|---|
10989 수 정렬하기 3 (0) | 2020.05.16 |
2751 - 수 정렬하기 2 (0) | 2020.05.11 |
[DP] 11052 카드 구매하기 (0) | 2020.05.05 |
다이나믹 프로그래밍 문제 정리 (0) | 2020.05.05 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 교착상태
- 재귀함수
- server side rendering
- react
- 알고리즘
- 최단경로
- Java
- 인접리스트
- 소프트웨어
- 운영체제
- javascript
- 스텍
- 입출력장치
- 이진탐색
- 클래스
- 퀵정렬
- 자료구조
- client side rendering
- C
- 동적프로그래밍
- stackframe
- 배열
- BFS
- 세마포어
- C++
- Stack
- 인접행렬
- 구조체
- dfs
- 병행프로세스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함