java自定义排序使用案例(屎盆子里找金子)

首先按照员工类的名字进行排序,如果名字一样的话就按照出生日期排序,具体使用案例如下:

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;


@SuppressWarnings("all")
public class genericTest02 {
    public static void main(String[] args) {
        Empoloyee xaoLi = new Empoloyee("小李子",12,new MyData(12,31,2000));
        Empoloyee xaoZhang = new Empoloyee("小张打",10,new MyData(12,3,2002));
        Empoloyee xaoWang = new Empoloyee("小王发",11,new MyData(7,31,1999));

        ArrayList<Empoloyee> empoloyees = new ArrayList<>();
        empoloyees.add(xaoLi);
        empoloyees.add(xaoZhang);
        empoloyees.add(xaoWang);

        empoloyees.sort(new Comparator<Empoloyee>() {
            @Override
            public int compare(Empoloyee o1, Empoloyee o2) {
                if(o1.getName().length() != o2.getName().length()){
                    return o1.getName().length() - o2.getName().length();
                }

                if(o1.getBrithday().getYear() != o2.getBrithday().getYear()){
                    return o1.getBrithday().getYear() - o2.getBrithday().getYear();
                }

                if(o1.getBrithday().getMonth() != o2.getBrithday().getMonth()){
                    return o1.getBrithday().getMonth() - o2.getBrithday().getMonth();
                }

                if(o1.getBrithday().getDay() != o2.getBrithday().getDay()){
                    return o1.getBrithday().getDay() - o2.getBrithday().getDay();
                }
                return 0;

            }
        });

        Iterator<Empoloyee> iterator = empoloyees.iterator();
        while (iterator.hasNext()) {
            Empoloyee next = iterator.next();
            System.out.println(next);
        }

    }
}


class Empoloyee {

    private String name;
    private double salary;
    private MyData brithday;

    public Empoloyee(String name, double salary, MyData brithday) {
        this.name = name;
        this.salary = salary;
        this.brithday = brithday;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public MyData getBrithday() {
        return brithday;
    }

    public void setBrithday(MyData brithday) {
        this.brithday = brithday;
    }

    @Override
    public String toString() {
        return "Empoloyee{" +
                "name='" + name + '\'' +
                ", salary=" + salary +
                ", brithday='" + brithday + '\'' +
                '}';
    }
}

@SuppressWarnings("all")
class MyData{
    private int month;
    private int day;
    private int year;

    public MyData(int month, int day, int year) {
        this.month = month;
        this.day = day;
        this.year = year;
    }

    @Override
    public String toString() {
        return "MyData{" +
                "month=" + month +
                ", day=" + day +
                ", year=" + year +
                '}';
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值