Comparator 多參數排序

一、需求
    假設現在有個如此的需求:需要對一個這樣的僱員列表進行排序,排序規則如下:
    1、首先級別最高的排在前面,
    2、如果級別相等,那麼按工資排序,工資高的排在前面,
    3、如果工資相當則按入職年數排序,入職時間最長的排在前面。

僱員對象包含級別、工資和入職年份,代碼如下:

package com.lyz.sort.bean;
 
import java.io.Serializable;
 
 
/**
 * 僱員信息
 * @author liuyazhuang
 *
 */
public class Employee implements Serializable {
 
    private static final long serialVersionUID = 4775629632953317597L;
      /**
     * ID
     */
    public int id;
    /**
     * 級別
     */
    public int level;
    /**
     * 工資
     */
    public int salary;
    /**
     * 入職年數
     */
    public int year;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    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 getYear() {
        return year;
    }
 
    public void setYear(int year) {
        this.year = year;
    }
 
    public Employee(int id, int level, int salary, int year) {
        this.id = id;
        this.level = level;
        this.salary = salary;
        this.year = year;
    }
}
二、實現Comparator接口
這裏我們實現Java.util.Comparator接口,用於對僱員列表進行排序,代碼如下:

package com.lyz.sort;
 
import java.util.Comparator;
 
import com.lyz.sort.bean.Employee;
 
/**
 * 核心排序類
 * @author liuyazhuang
 *
 */
public class EmpComparator implements Comparator<Employee> {
 
    @Override
    public int compare(Employee employee1, Employee employee2) {
          int cr = 0;
          //按級別降序排列
          int a = employee2.getLevel() - employee1.getLevel();
          if (a != 0) {
              cr = (a > 0) ? 3 : -1;
          } else {
              //按薪水降序排列
              a = employee2.getSalary() - employee1.getSalary();
              if (a != 0) {
                  cr = (a > 0) ? 2 : -2;
              } else {
                  //按入職年數降序排列
                  a = employee2.getYear() - employee1.getYear();
                  if (a != 0) {
                      cr = (a > 0) ? 1 : -3;
                  }
              }
          }
          return cr;
    }
 
}
三、驗證排序結果
下面用一個單元測試,來驗證排序結果是否正確

package com.lyz.sort.test;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
import org.junit.Test;
 
import com.lyz.sort.EmpComparator;
import com.lyz.sort.bean.Employee;
 
/**
 * 測試排序類
 * 
 * @author liuyazhuang
 *
 */
public class SortTest {
    @Test
    public void sortTest() throws Exception {
        List<Employee> employeeList = new ArrayList<Employee>() {
            {
                add(new Employee(1, 9, 10000, 10));
                add(new Employee(2, 9, 12000, 7));
                add(new Employee(3, 5, 10000, 12));
                add(new Employee(4, 5, 10000, 6));
                add(new Employee(5, 3, 5000, 3));
                add(new Employee(6, 1, 2500, 1));
                add(new Employee(7, 5, 8000, 10));
                add(new Employee(8, 3, 8000, 2));
                add(new Employee(9, 1, 3000, 5));
                add(new Employee(10, 1, 2500, 4));
                add(new Employee(11, 2, 2000, 4));
            }
        };
        Collections.sort(employeeList, new EmpComparator());
        System.out.println("ID\tLevel\tSalary\tYears");
        System.out.println("=============================");
        for (Employee employee : employeeList) {
            System.out.printf("%d\t%d\t%d\t%d\n", employee.getId(), employee.getLevel(), employee.getSalary(),
                    employee.getYear());
        }
        System.out.println("=============================");
    }
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章