package collection;
import character.Hero;
import java.util.*;
public class TestCollection {
public static void main(String[] args) {
Random r = new Random();
List<Hero> heros = new ArrayList<>();
for(int i=0;i<10;i++){
heros.add(new Hero("hero-"+i,r.nextInt(100),r.nextInt(100)));
}
System.out.println("初始化集合");
System.out.println(heros);
Collections.sort(heros);//Hero实现Comparable接口return this.hp>hp?1:-1;
Comparator<Hero> c = new Comparator<Hero>() {
@Override
public int compare(Hero o1, Hero o2) {
return o1.hp>=o2.hp?1:-1;//从小到大,o1大于o2为正,大于
}
};
//Collections.sort(heros,c);
System.out.println("按照血量排序后集合(小->大):");
System.out.println(heros);
}
}
java基础——根据指定属性排序
最新推荐文章于 2022-11-07 09:22:11 发布