Collections.sort()で数値を降順にするサンプルコード
サンプルコード
package com.arkgame;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
List<Integer> cft = new ArrayList<>();
cft.add(11);
cft.add(22);
cft.add(33);
cft.add(44);
cft.add(45);
//数値を降順にする
Collections.sort(cft, new java.util.Comparator<Integer>() {
@Override
public int compare(Integer a, Integer b) {
return b - a;
}
});
System.out.println("実行結果:");
for (Integer fc : cft) {
System.out.println(fc);
}
}
}
実行結果:
45
44
33
22
11