List元素是对象,根据对象的某个属性排序

1 篇文章 0 订阅

实体类

import java.util.Date;

public class User {

    private Integer userId;

    private String username;

    private String password;

    private Date birthday;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public User(Integer userId, String username, String password, Date birthday) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.birthday = birthday;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", birthday=" + birthday +
                '}';
    }
}

根据生日日期倒序

import com.example.demo.domain.User;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class TestListSort {

    private static void ListSort(List<User> list) {

        Collections.sort(list, new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                try {
                    Date dt1 = u1.getBirthday();
                    Date dt2 = u2.getBirthday();
                    if (dt1.getTime() < dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() > dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }


    public static void main(String[] args) {
        List<User> users = new ArrayList<>();
        User user1 = new User(1, "张三", "111111", string2Date("2000-09-17 22:33:45"));
        User user2 = new User(2, "李四", "222222", string2Date("2002-06-15 22:33:45"));
        User user3 = new User(3, "王五", "333333", string2Date("2002-06-03 22:33:45"));
        User user4 = new User(4, "小明", "444444", string2Date("1993-09-17 22:33:45"));
        User user5 = new User(5, "蛋蛋", "555555", string2Date("2010-06-19 22:33:45"));
        users.add(user1);
        users.add(user2);
        users.add(user3);
        users.add(user4);
        users.add(user5);
        ListSort(users);
        System.err.println(users);
    }


    public static Date string2Date(String str){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = null;
        try {
            parse = dateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值