对复杂的排序需求的处理
EmployeeSortTest 测试类
package v1ch06.interfaces;
import java.util.*;
/**
* This program demonstrates the use of the Comparable interface.
* @version 1.30 2004-02-27
* @author Cay Horstmann
*/
public class EmployeeSortTest
{
public static void main(String[] args)
{
// 数组排序
Employee[] staff = new Employee[9];
List<Employee> list = new ArrayList<>();
staff[0] = new Employee("05", "05", "06", list);
staff[4] = new Employee("04", "04", "05", list);
staff[3] = new Employee("03", "03", "04", list);
staff[1] = new Employee("02", "02", "03", list);
staff[2] = new Employee("01", "01", "02", list);
staff[5] = new Employee("06", "06", "07", list);
staff[8] = new Employee("07", "07", "", list);
staff[7] = new Employee("08", "08", "017", list);
staff[6] = new Employee("09", "09", "017", list);
for (Employee employee : staff) {
list.add(employee);
}
Arrays.sort(staff);
for (Employee e : staff)
System.out.println("name=" + e.getName() );
//列表排序
// for (Employee employee : staff) {
// list.add(employee);
// }
//
// list.sort(new EmployeeCom(list));
//
//
// for (Employee e : list)
// System.out.println("name=" + e.getName() );
}
}
EmployeeCom
package v1ch06.interfaces;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
class EmployeeCom implements Comparator<Employee> {
List<Employee> list;
public EmployeeCom(List<Employee> list) {
this.list = list;
}
public int compare(Employee var1, Employee var2) {
return isParent(var1, var2) ? -1 : 1;
}
/**
* 判断对方是不是我的上级
* @param other
* @return
*/
private boolean isParent(Employee me, Employee other) {
Employee parent = me;
while (true && parent != null) {
if (other.getMobile().equalsIgnoreCase(parent.getPreMobile())) {
return true;
} else {
parent = getParent(parent);
}
}
return false;
}
private Employee getParent(Employee employee) {
Optional<Employee> optional = this.list.stream().filter(item -> item.getMobile().equals(employee.getPreMobile())).findFirst();
if (optional.isPresent()) {
return optional.get();
} else {
return null;
}
}
}
Employee
package v1ch06.interfaces;
import java.util.List;
import java.util.Optional;
public class Employee implements Comparable<Employee>
{
private String name;
private String mobile;
private String preMobile;
private List<Employee> list;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getPreMobile() {
return preMobile;
}
public void setPreMobile(String preMobile) {
this.preMobile = preMobile;
}
public List<Employee> getList() {
return list;
}
public void setList(List<Employee> list) {
this.list = list;
}
public Employee(String name, String mobile, String preMobile, List<Employee> list) {
this.name = name;
this.preMobile = preMobile;
this.mobile = mobile;
this.list = list;
}
/**
* Compares employees by salary
* @param other another Employee object
* @return a negative value if this employee has a lower salary than
* otherObject, 0 if the salaries are the same, a positive value otherwise
*/
@Override
public int compareTo(Employee other)
{
return isParent(other) ? -1 : 1;
}
/**
* 判断对方是不是我的上级
* @param other
* @return
*/
private boolean isParent(Employee other) {
Employee parent = this;
while (true && parent != null) {
if (other.getMobile().equalsIgnoreCase(parent.getPreMobile())) {
return true;
} else {
parent = getParent(parent);
}
}
return false;
}
private Employee getParent(Employee employee) {
Optional<Employee> optional = this.list.stream().filter(item -> item.getMobile().equals(employee.getPreMobile())).findFirst();
if (optional.isPresent()) {
return optional.get();
} else {
return null;
}
}
}