티스토리 뷰
1. Pair 클래스 구현 후, 목적에 맞게 정렬하기 예제
예시) second 내림차순, first 오름차순 예제
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
public static void main(String[] args) {
Vector<Pair> result = new Vector<Pair>();
// second 내림차순, first 오름차순 예제
Collections.sort(result, new Comparator<Pair>() {
public int compare(Pair p1, Pair p2) {
// second 먼저 내림차순
if(p1.second > p2.second) {
return -1;
}
// 같으면 first 오름차순
else if(p1.second == p2.second) {
return Integer.compare(p1.first, p2.first);
}
else {
return 1;
}
}
});
}
class Pair {
int first;
double second;
Pair(int first, double second) {
this.first = first;
this.second = second;
}
}
참고 https://hsdevelopment.tistory.com/474
'java' 카테고리의 다른 글
컬렉션 프레임웍 Collections Framework (0) | 2022.05.09 |
---|---|
네트워킹(Networking) (0) | 2022.05.08 |
예외처리(exception handling) (0) | 2022.05.01 |
싱글톤패턴 (0) | 2022.04.26 |
메모리는 어떻게 할당하는가 (0) | 2022.01.02 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday