phen0men phen0men - 5 months ago 13
Java Question

How to sort ArrayList of extend classes?

Here's a main class

Employee.java
and 2 another classes that extend main class.
ArrayList<Employee>
contains objects from
createFixedEmployee.java
and
createPerHourEmployee
. How to sort objects in
ArrayList
by salary, and if some objects got same salary then sort their names alphabetically?

I tried to use
Comparator.comparing();
but doesn't work, i get an error
Cannot resolve method getMonthSalary();
Here's a code:

createPerHourEmployee.java

public class createPerHourEmployee extends Employee {
private double salary;

public createPerHourEmployee() {}

public double getSalaryPerHour() { return this.salary; }

public double setSalaryPerHour(double value) {
return this.salary = value;
}


public createPerHourEmployee (int _id, String _name, double _salary) {
setEmployeeID(_id);
setEmployeeName(_name);
this.salary = _salary;
}

public double getMonthSalary() {
return salary * (20 * 0.8);
}

public String toString() {
return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryPerHour();
}
}


createFixedEmployee.java

public class createFixedEmployee extends Employee {
private double salary;

public createFixedEmployee() {}

public double getSalaryFixed() {
return this.salary;
}

public double setSalaryFixed(double value) {
return this.salary = value;
}

public createFixedEmployee(int _id, String _name, double _salary) {
setEmployeeID(_id);
setEmployeeName(_name);
this.salary = _salary;
}

public double getMonthSalary() {
return this.salary;
}

public String toString() {
return getEmployeeID() + ", " + getEmployeeName() + ", " + getSalaryFixed();
}

}


Employee.java

import java.util.ArrayList;
import java.util.Comparator;

public abstract class Employee {
private int base_id;
private String base_name;

public int getEmployeeID () {
return this.base_id;
}

public int setEmployeeID (int value) {
return this.base_id = value;
}

public String getEmployeeName () {
return this.base_name;
}

public String setEmployeeName(String value) {
return this.base_name = value;
}

public abstract double getMonthSalary();

public static void main(String[] argc) {
ArrayList<Employee> Employee = new ArrayList<Employee>();
Employee.add(new createPerHourEmployee(1, "asd", 1300));
Employee.add(new createFixedEmployee(7, "asds", 14025));
Employee.add(new createPerHourEmployee(2, "nikan", 1230));
Employee.add(new createPerHourEmployee(3, "nikalo", 12330));
Employee.add(new createFixedEmployee(6, "aaaa", 14025));
Employee.add(new createFixedEmployee(4, "nikaq", 140210));
Employee.add(new createFixedEmployee(5, "nikas", 124000));
Employee.add(new createFixedEmployee(6, "nikab", 14025));


Employee.sort(Comparator.comparing(Employee::getMonthSalary)
.thenComparing(Employee::getEmployeeName)); // here is an error

}
}


So I have tried to use

Employee.sort(Comparator.comparing(createFixedEmployee::getMonthSalary).
thenComparing(createFixedEmployee::getEmployeeName));


It works fine, but I need to sort for all classes, not only one class.

Answer

When sorting a List<T>, you can definitely use functional references to any of T's ancestors methods. The issue here is with the name you give to the list - You've named the local variable Employee, which hides the name of the type Employee when you try to create the function references in the Comparator. Just give it a better name, and you should be fine. E.g.:

ArrayList<Employee> employeeList = new ArrayList<Employee>();
// Or better yet: List<Employee> employeeList = new ArrayList<>();

employeeList.add(new createPerHourEmployee(1, "asd", 1300));
employeeList.add(new createFixedEmployee(7, "asds", 14025));
employeeList.add(new createPerHourEmployee(2, "nikan", 1230));
employeeList.add(new createPerHourEmployee(3, "nikalo", 12330));
employeeList.add(new createFixedEmployee(6, "aaaa", 14025));
employeeList.add(new createFixedEmployee(4, "nikaq", 140210));
employeeList.add(new createFixedEmployee(5, "nikas", 124000));
Employee.add(new createFixedEmployee(6, "nikab", 14025));


employeeList.sort(Comparator.comparing(Employee::getMonthSalary)
      .thenComparing(Employee::getEmployeeName));
Comments