* 정렬할 클래스: Comparable 구현 - compareTo(Object) 메서드
* 정렬시에는 Arrays.sort(arr);
아래는 옛날에 작성했던 소스 중 정렬 부분만 추출한것
public class Task implements Comparable {
private int i;
private int d;
private int w;
public Task(int ii, int dd, int ww) {
i = ii;
d = dd;
w = ww;
}
public int getI() {
return i;
}
public int getD() {
return d;
}
public int getW() {
return w;
}
// descending order
public int compareTo( Object o ) {
Task other = (Task)o;
if ( w < other.getW() ) return 1;
if ( w > other.getW() ) return -1;
return 0;
}
}
import java.util.*;
public class Schedule {
public static void main( String[] args ) {
Task arrT[];
arrT = new Task[7];
arrT[0] = new Task(1, 4, 10);
arrT[1] = new Task(2, 2, 20);
arrT[2] = new Task(3, 4, 30);
arrT[3] = new Task(4, 3, 40);
arrT[4] = new Task(5, 1, 50);
arrT[5] = new Task(6, 4, 60);
arrT[6] = new Task(7, 6, 70);
// sort arrT descending order of w
Arrays.sort(arrT);
}
}