简体   繁体   English

在Java ArrayList中排序对象块

[英]Sort block of Objects in Java ArrayList

I have a block of data, and I want to sort them according to age and salary. 我有一块数据,我想根据年龄和工资对它们进行排序。

Object Employee has: 对象Employee有:

  • Name 名称
  • Designation 指定
  • Age 年龄
  • Salary 薪水

I want to sort then so that the max age will be on top. 我想排序,以便最大年龄将在最前面。 But if the age is same of two persons,then it will compare the salary between them, and then for whom Age and Salary is high, he will be on top of the ArrayList. 但如果两个人的年龄相同,那么它将比较他们之间的薪水,然后对于年龄和薪水高的人,他将在ArrayList之上。

Here in the top list accordingly: Bob, Tim, Tom. 相应地在顶部列表中:Bob,Tim,Tom。 then in the list: Alex, Sam, Craig and so on. 然后在列表中:Alex,Sam,Craig等。

List<Employee> listEmployees = new ArrayList<Employee>(); 
listEmployees.add(new Employee("Tom", "Developer", 45, 80000));
listEmployees.add(new Employee("Sam", "Designer", 30, 75000));
listEmployees.add(new Employee("Bob", "Designer", 45, 134000));
listEmployees.add(new Employee("Peter", "Programmer", 25, 60000));
listEmployees.add(new Employee("Tim", "Designer", 45, 130000));
listEmployees.add(new Employee("Craig", "Programmer", 30, 52000));
listEmployees.add(new Employee("Anne", "Programmer", 25, 51000));
listEmployees.add(new Employee("Alex", "Designer", 30, 120000));

I have tried using 我试过用

Collections.sort(Employees , new Comparator<listEmployees >() {
@Override
public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
return (rhs.getAge() - lhs.getAge());
}
});

also I have tried 我也试过了

Collections.sort(Employees , new Comparator<listEmployees >() {
@Override
public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
return ((rhs.getAge() - lhs.getAge()) - (rhs.getSalary() - lhs.getSalary()));
}
});

but unable to get correct data. 但无法获得正确的数据。 Does anybody have any ideas? 有人有什么想法吗?

Make the comparator as natural as possible, because it gives you clear idea how the employees are compared. 使比较器尽可能自然,因为它可以让您清楚地了解员工的比较方式。 For example, 例如,

Collections.sort(Employees , new Comparator<listEmployees >() {
  public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
    if (lhs.getAge() < rhs.getAge()) {
      return 1;
    } else if (lhs.getAge() > rhs.getAge()) {
      return -1;
    } else {
      if (lhs.getSalary() < rhs.getSalary()) {
        return 1;
      } else if (lhs.getSalary() > rhs.getSalary()) {
        return -1;
      } else {
        return 0;
      }
    }
  }
});

Once you do this, you can optimize the comparator. 完成此操作后,您可以优化比较器。 For example, 例如,

Collections.sort(Employees , new Comparator<listEmployees >() {
  public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
    return (rhs.getAge() == lhs.getAge()) ? (rhs.getSalary() - lhs.getSalary()) : (rhs.getAge() - lhs.getAge());
  }
});

As you can see, we compare the salaries only when the ages are the same. 如您所见,我们仅在年龄相同时才比较工资。 In your code, you compare the salaries even when the ages are not the same. 在您的代码中,即使年龄不同,您也可以比较工资。 This is the problem. 这就是问题。

Consider using guava's ordering utilities to create a compound comparator. 考虑使用guava的排序实用程序来创建复合比较器。 In this specific case, it appears you need 2 comparators compounded together, first age then salary. 在这种特殊情况下,你需要将2个比较器组合在一起,首先是年龄然后是工资。

Maybe try this way 也许试试这种方式

Collections.sort(listEmployees , new Comparator<listEmployees >() {
    @Override
    public int compare(EmployeeModelCls lhs, EmployeeModelCls rhs) {
        if (rhs.getAge() - lhs.getAge() == 0)
            return rhs.getSalary() - lhs.getSalary();

        return (rhs.getAge() - lhs.getAge());
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM