List<User> students = new ArrayList<User>(); User user1 = new User();
user1.setAge(112);
user1.setUserName("小米");
user1.setMoney(118.55);
students.add(user1); User user2 = new User();
user2.setAge(82);
user2.setUserName("小王");
user2.setMoney(88.55);
students.add(user2); User user3 = new User();
user3.setAge(100);
user3.setUserName("小李");
user3.setMoney(110.65);
students.add(user3); User user4 = new User();
user4.setAge(99);
user4.setUserName("小赵");
user4.setMoney(99.5);
students.add(user4); Collections.sort(students, new Comparator<User>() {
@Override
public int compare(User o1, User o2) {
//降序
/*if(o1.getMoney()>o2.getMoney()){
return -1;
}
if(o1.getMoney()<o2.getMoney()){
return 1;
}
return 0;*/ //升序
if(o1.getMoney()>o2.getMoney()){
return 1;
}
if(o1.getMoney()<o2.getMoney()){
return -1;
}
return 0; /*int i = o1.getAge() - o2.getAge();
return i;*/
}
});
for(User stu : students){
System.out.println("score:" + stu.getUserName() + ":age:" + stu.getAge()+":money:"+stu.getMoney());
}
简单类型list排序
List<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(5);
nums.add(1);
nums.add(0);
System.out.println(nums);
Collections.sort(nums);
System.out.println(nums);
list集合随机显示(实体类)
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < 5; i++) {
list.add(i);
}
Collections.shuffle(list);
for(Integer yy:list){
System.out.println("随机:"+yy);
}