实现具有特定条件的比较器

时间:2014-01-09 05:07:01

标签: java sorting comparator

我需要实现Comparator根据特定条件对Employee个对象进行排序。

 class Employee
 {
      int employeeId;
      String employeeName;

 }

所以我手边已经有了List的排序列表,我修改了排序顺序如下

  1. 维护排序顺序
  2. 如果employeeName对象中的employeeName为'Rock Hudson',请将其添加到列表顶部。
  3. >输入:

      

    1 Alan Border

         2,贝蒂爱德华兹

         

    3 Rock Hudson

         

    4昆汀塔伦蒂诺

         

    5伍迪艾伦

    >输出

      

    3 Rock Hudson

         

    1 Alan Border

         2,贝蒂爱德华兹

         

    4昆汀塔伦蒂诺

         

    5伍迪艾伦

3 个答案:

答案 0 :(得分:3)

你可以这样做

public static class Employee {
  // a constructor
  public Employee(int id, String name) {
    this.employeeId = id;
    this.employeeName = name;
  }

  // should be private and use getters and setters.
  int employeeId;
  String employeeName;

  // A special Comparator
  public static Comparator<Employee> rockHudsonComparator = new Comparator<Employee>() {
    @Override
    public int compare(Employee a, Employee b) {
      if (a.employeeName.equals("Rock Hudson")) {
        if (b.employeeName.equals("Rock Hudson")) {
          return 0;
        }
        return -1;
      } else if (b.employeeName.equals("Rock Hudson")) {
        return 1;
      }
      return a.employeeName
          .compareTo(b.employeeName);
    }
  };

  public String toString() {
    return "" + employeeId + " " + employeeName;
  }
}

public static void main(String[] args) {
  java.util.List<Employee> al = new java.util.ArrayList<Employee>();
  al.add(new Employee(1, "Alan Border"));
  al.add(new Employee(2, "Betty Edwards"));
  al.add(new Employee(3, "Rock Hudson"));
  al.add(new Employee(4, "Quentin Tarantino"));
  al.add(new Employee(5, "Woody Allen"));
  al.add(new Employee(6, "Rock Hudson")); // check the sort is stable.
  System.out.println("Before sort");
  System.out.println(al);
  java.util.Collections.sort(al,
      Employee.rockHudsonComparator);
  System.out.println("After sort");
  System.out.println(al);
}

哪个输出

[1 Alan Border, 2 Betty Edwards, 3 Rock Hudson, 4 Quentin Tarantino, 5 Woody Allen, 6 Rock Hudson]
[3 Rock Hudson, 6 Rock Hudson, 1 Alan Border, 2 Betty Edwards, 4 Quentin Tarantino, 5 Woody Allen]

答案 1 :(得分:0)

为这种情况制定一个特殊条件,在每个方向上进行测试:

Comparator<Employee> c = new Comparator<Employee>() {
    public int compare(Employee a, Employer b) {
        if (a.getName().equals("Rock Hudson"))
             return -1;
        if (b.getName().equals("Rock Hudson"))
             return 1;
        return a.getName().compareTo(b.getName());
    }
}

答案 2 :(得分:0)

public class MyEmployeeComparator implements Comparator<Employee> {

    private final String alwaysTopEmployee;

    public MyEmployeeComparator(final String alwaysTopEmployee) {
        this.alwaysTopEmployee = alwaysTopEmployee;
    }

    @Override
    public int compare(Employee o1, Employee o2) {
        if (o2.getName().equals(alwaysTopEmployee) && o1.getName().equals(alwaysTopEmployee))
            return 0;
        if (o1.getName().equals(alwaysTopEmployee))
            return -1;
        if (o2.getName().equals(alwaysTopEmployee))
            return 1;

        return o1.getName().compareTo(o2.getName());
    }
}

并致电:

Collections.sort(stuff, new MyEmployeeComparator("Rock Hudson"));