员工按照工资从大到小排列,java
时间: 2023-11-21 12:19:36 浏览: 79
您好,可以使用Java中的Arrays类的sort方法来对员工按照工资从大到小排序,示例代码如下:
```java
import java.util.Arrays;
public class Employee {
private String name;
private double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public double getSalary() {
return salary;
}
public static void main(String[] args) {
Employee[] employees = {new Employee("Alice", 5000), new Employee("Bob", 7000), new Employee("Charlie", 6000)};
//按照工资从大到小排序
Arrays.sort(employees, (e1, e2) -> Double.compare(e2.getSalary(), e1.getSalary()));
//输出排序结果
for (Employee employee : employees) {
System.out.println(employee.getName() + " " + employee.getSalary());
}
}
}
```
运行结果:
```
Bob 7000.0
Charlie 6000.0
Alice 5000.0
```
这里使用了lambda表达式来定义比较器,也可以使用实现Comparator接口的类来定义比较器。
阅读全文