• java collection.sort()根据时间排序list


    首先:定义bean

    然后:定义比较器

    最后:测试使用

    一.userBean

    package com.butterfly.Class;
    
    public class user {
        private String name;
        private String sex;
        private int age;
        private String birthday;
        
        public user(String name,String sex,int age,String birthday){
            this.name = name;
            this.sex = sex;
            this.age = age;
            this.birthday = birthday;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getSex() {
            return sex;
        }
        public void setSex(String sex) {
            this.sex = sex;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public String getBirthday() {
            return birthday;
        }
        public void setBirthday(String birthday) {
            this.birthday = birthday;
        }
        
        
    }

    二.定义比较器

    package com.butterfly.Class;
    
    import java.util.Comparator;
    
    public class sortClass implements Comparator{
        public int compare(Object arg0,Object arg1){
            user user0 = (user)arg0;
            user user1 = (user)arg1;
            int flag = user0.getBirthday().compareTo(user1.getBirthday());
            return flag;
        }
    }

    三.测试使用

    package com.butterfly.Class;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    
    public class testClass {
        public static void main(String[] args){
            List userList = new ArrayList();
            userList.add(new user("吴光雷","男",25,"19871028"));
            userList.add(new user("吴桥","男",22,"19900923"));
            userList.add(new user("陆晓敏","男",25,"19870728"));
            userList.add(new user("王佳俊","男",23,"19890728"));
            userList.add(new user("秦菁","女",23,"19891212"));
            
            sortClass sort = new sortClass();
            Collections.sort(userList,sort);
            for(int i=0;i<userList.size();i++){
                user temp = (user)userList.get(i);
                System.out.println("姓名:"+temp.getName()+",生日:"+temp.getBirthday());
            }
        }
    }

     输出结果:

    姓名:陆晓敏,生日:19870728

    姓名:吴光雷,生日:19871028

    姓名:王佳俊,生日:19890728

    姓名:秦菁,生日:19891212

    姓名:吴桥,生日:19900923

    出自:http://blog.csdn.net/learner9023/article/details/8432508

  • 相关阅读:
    git简单使用
    简单Spring和mybatis整合配置文件
    ASP.NET程序开发范例宝典
    C# DataSet和DataTable详解
    AOP
    匿名内部类
    数据库事务
    mybatis
    线程池
    单例模式
  • 原文地址:https://www.cnblogs.com/donetbaoxj320/p/3679342.html
Copyright © 2020-2023  润新知