import java.util.Arrays;
import java.util.Comparator;
// ユニットクラス
class Unit {
float threshold=0;
float value=0;
int index;
Unit() {}
}
public class Foo implements Comparator {
static Unit u[];
public static void main(String args[]) {
// ユニットの生成と初期値決定
u=new Unit[50];
for (int i=0; i<u.length; i++) {
u[i]=new Unit();
u[i].value=(int)(Math.random()*99);
}
// コンパレータの生成
Comparator u_cmp=new Foo();
// ここで上のコンパレータを指定して Array.sort() を呼び
// 出すと指定したクラス内の compare メソッドが呼ばれる
Arrays.sort(u, u_cmp);
}
public int compare (Object _a, Object _b) {
Unit a=(Unit)_a, b=(Unit)_b;
if (a.value>b.value) return -1; // 大きい方が上位になる
else if (a.value<b.value) return 1;
return 0;
}
}
|