Comparator
use :
Class로 정의한 Object를 사용자의 용도에 맞게 정렬 할 때 사용
import
import java.util.Comparator
implement
class Persion implements Comparator<Persion> {
String name;
String position;
int age;
public Persion(String name, String position, int age) {
this.name = name;
this.position = position;
this.age = age;
}
@Override
public int compare(Persion p1, Persion p2) {
if (p1.age > p2.age) {
return 1; //오름차순
} else if (p1.age < p2.age) {
return -1; //내림차순
} else {
return 0;
}
}
}
sort
private static ArrayList<Persion> mTeam;
mTeam = new ArrayList<Persion>();
Persion persion = new Persion(name, position, age);
mTeam.add(persion);
Collections.sort(mTeam, persion);
Collections.reverse(mTeam);