lotus

贵有恒何必三更眠五更起 最无益只怕一日曝十日寒

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
  1846 随笔 :: 0 文章 :: 109 评论 :: 288万 阅读

 

 
public class Employee {
    private int level;
    private int salary;
    private int years;

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public int getSalary() {
        return salary;
    }

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

    public int getYears() {
        return years;
    }

    public void setYears(int years) {
        this.years = years;
    }

    @Override
    public String toString() {
        return "Employee [level=" + level + ", salary=" + salary + ", years="
                + years + "]";
    }
}


 

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        List<Employee> list = new ArrayList<Employee>();

        Employee e = new Employee();
        e.setLevel(1);
        e.setSalary(300);
        e.setYears(4);
        list.add(e);

        e = new Employee();
        e.setLevel(1);
        e.setSalary(500);
        e.setYears(4);
        list.add(e);

        e = new Employee();
        e.setLevel(2);
        e.setSalary(500);
        e.setYears(4);
        list.add(e);

        e = new Employee();
        e.setLevel(1);
        e.setSalary(300);
        e.setYears(5);
        list.add(e);
//        Collections.sort(list, new Comparator<Employee>() {
//            public int compare(Employee e2, Employee e1) {
//                int a = e1.getLevel() - e2.getLevel();
//                if (a != 0) {
//                    return a > 0 ? -1 : 1;
//                }
//                a = e1.getSalary() - e2.getSalary();
//                if (a != 0) {
//                    return a > 0 ? -1 : 1;
//                }
//                a = e1.getYears() - e2.getYears();
//                return a > 0 ? -1 : 1;
//            }
//        });
        Collections.sort(list, new Comparator<Employee>() {
            public int compare(Employee e1, Employee e2) {
                int a = e1.getLevel() - e2.getLevel();
                if (a != 0) {
                    return a > 0 ? 1 : -1;
                }
                a = e1.getSalary() - e2.getSalary();
                if (a != 0) {
                    return a > 0 ? 1 : -1;
                }
                a = e1.getYears() - e2.getYears();
                return a > 0 ? 1 : -1;
            }
        });

//  Employee [level=2, salary=500, years=4]
//  Employee [level=1, salary=500, years=4]
//  Employee [level=1, salary=300, years=5]
//  Employee [level=1, salary=300, years=4]

// Employee [level=1, salary=300, years=4]
// Employee [level=1, salary=300, years=5]
// Employee [level=1, salary=500, years=4]
// Employee [level=2, salary=500, years=4]

//          [level=1, salary=300, years=4]
//          [level=1, salary=300, years=5]
//          [level=1, salary=500, years=4]
//          [level=2, salary=500, years=4]


        for (Employee em : list) {
            System.out.println(em);
        }

    }
}
posted on   白露~  阅读(1513)  评论(0编辑  收藏  举报
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示