android根据内容对list重新排序

首先:定义bean

然后:定义比较器

最后:测试使用

一.userBean

[java]  view plain copy
  1. package com.butterfly.Class;  
  2.   
  3. public class user {  
  4.     private String name;  
  5.     private String sex;  
  6.     private int age;  
  7.     private String birthday;  
  8.       
  9.     public user(String name,String sex,int age,String birthday){  
  10.         this.name = name;  
  11.         this.sex = sex;  
  12.         this.age = age;  
  13.         this.birthday = birthday;  
  14.     }  
  15.     public String getName() {  
  16.         return name;  
  17.     }  
  18.     public void setName(String name) {  
  19.         this.name = name;  
  20.     }  
  21.     public String getSex() {  
  22.         return sex;  
  23.     }  
  24.     public void setSex(String sex) {  
  25.         this.sex = sex;  
  26.     }  
  27.     public int getAge() {  
  28.         return age;  
  29.     }  
  30.     public void setAge(int age) {  
  31.         this.age = age;  
  32.     }  
  33.     public String getBirthday() {  
  34.         return birthday;  
  35.     }  
  36.     public void setBirthday(String birthday) {  
  37.         this.birthday = birthday;  
  38.     }  
  39.       
  40.       
  41. }  


二.定义比较器

[java]  view plain copy
  1. package com.butterfly.Class;  
  2.   
  3. import java.util.Comparator;  
  4.   
  5. public class sortClass implements Comparator{  
  6.     public int compare(Object arg0,Object arg1){  
  7.         user user0 = (user)arg0;  
  8.         user user1 = (user)arg1;  
  9.         int flag = user0.getBirthday().compareTo(user1.getBirthday());  
  10.         return flag;  
  11.     }  
  12. }  


三.测试使用

[java]  view plain copy
  1. package com.butterfly.Class;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Collections;  
  5. import java.util.List;  
  6.   
  7. public class testClass {  
  8.     public static void main(String[] args){  
  9.         List userList = new ArrayList();  
  10.         userList.add(new user("吴光雷","男",25,"19871028"));  
  11.         userList.add(new user("吴桥","男",22,"19900923"));  
  12.         userList.add(new user("陆晓敏","男",25,"19870728"));  
  13.         userList.add(new user("王佳俊","男",23,"19890728"));  
  14.         userList.add(new user("秦菁","女",23,"19891212"));  
  15.           
  16.         sortClass sort = new sortClass();  
  17.         Collections.sort(userList,sort);  
  18.         for(int i=0;i<userList.size();i++){  
  19.             user temp = (user)userList.get(i);  
  20.             System.out.println("姓名:"+temp.getName()+",生日:"+temp.getBirthday());  
  21.         }  
  22.     }  
  23. }  


 输出结果:

姓名:陆晓敏,生日:19870728
姓名:吴光雷,生日:19871028
姓名:王佳俊,生日:19890728
姓名:秦菁,生日:19891212
姓名:吴桥,生日:19900923

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值