package test.user.taojq;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
class User {
String name;
String age;
public User(String name,String age){
this.name=name;
this.age=age;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//测试类
public class SortTest {
public static void main(String[] args){
List userlist=new ArrayList();
userlist.add(new User("dd","4"));
userlist.add(new User("aa","1"));
userlist.add(new User("ee","5"));
userlist.add(new User("bb","2"));
userlist.add(new User("ff","5"));
userlist.add(new User("cc","3"));
userlist.add(new User("gg","6"));
// ComparatorUser comparator=new ComparatorUser();
// Collections.sort(userlist, comparator);
Collections.sort(userlist, new Comparator<User>(){
public int compare(User user0, User user1) {
int flag=user0.getAge().compareTo(user1.getAge());
if(flag==0){
return user0.getName().compareTo(user1.getName());
}else{
return flag;
}
}
});
for (int i=0;i<userlist.size();i++){
User user_temp=(User)userlist.get(i);
System.out.println(user_temp.getAge()+","+user_temp.getName());
}
}
}
对对象的某些属性进行排序
最新推荐文章于 2024-08-16 23:08:28 发布