デフォルトはacending
acending以外にしたい場合、comparatorを作って渡す
sortは、comparatorのcompare結果が0以下となるように並び替える
昇順にしたければp1-p2、降順にしたければp2-p1
public static void main(String[] args) {
List<Integer> li = new ArrayList();
li.add(1);
li.add(100);
li.add(3);
Comparator c = new Outer().new DecendingComparator();
Collections.sort(li);
for(Integer i:li) {
System.out.println(i);
}
System.out.println("----------------------");
Collections.sort(li, c);
for(Integer i:li) {
System.out.println(i);
}
}
class DecendingComparator implements Comparator<Integer> {
@Override
public int compare(Integer i,Integer j) {
return j - i;
}
}
1
3
100
----------------------
100
3
1