1. 정렬하고자 하는 객체에 Comparable 인터페이스를 구현한다.
2. Collections.sort 함수로 정렬한다.
public class SwitchInfo implements Comparable<SwitchInfo> {
private int id;
private double power;
public SwitchInfo(int id ){
this.id = id;
}
public double getPower() {
return power;
}
public void setPower(double power) {
this.power = power;
}
@Override
public int compareTo(SwitchInfo si) {
if (this.power > si.power) { // 내림차순 , 오름차순으로 하려면 < 으로~
return -1;
} else if (this.power == si.power) {
return 0;
} else {
return 1;
}
}
}
ArrayList<SwitchInfo> switches = group.getSwitches();
Collections.sort(switches); // Power 를 내림차순으로 정렬
'Java' 카테고리의 다른 글
| 자바언어에서 동기화의 어려움 (1) (0) | 2015.05.20 |
|---|---|
| 자바의 런타임 계열 예외와 checked 예외 (0) | 2015.05.15 |
| 자바 스케쥴링 & 타이머 방법들 (0) | 2015.05.14 |
| Java Time/ Date / Calendar example (0) | 2015.05.13 |
| Java Time,Data 클래스의 문제점과 JAVA 8 (0) | 2015.05.13 |