Я пытаюсь отсортировать список сотрудников по дате их вступления.Ниже приведен класс моего сотрудника.
import java.time.LocalDate;
public class Employee {
private String name;
private String empID;
private Designation designation;
private LocalDate dateOfJoining;
private int monthlySalary;
public Employee(String name, String empID, Designation designation, LocalDate dateOfJoining, int monthlySalary) {
super();
this.name = name;
this.empID = empID;
this.designation = designation;
this.dateOfJoining = dateOfJoining;
this.monthlySalary = monthlySalary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
public Designation getDesignation() {
return designation;
}
public void setDesignation(Designation designation) {
this.designation = designation;
}
public LocalDate getDOJ() {
return dateOfJoining;
}
public void setDOJ(LocalDate dOJ) {
dateOfJoining = dOJ;
}
public int getMonthlySalary() {
return monthlySalary;
}
public void setMonthlySalary(int monthlySalary) {
this.monthlySalary = monthlySalary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((dateOfJoining == null) ? 0 : dateOfJoining.hashCode());
result = prime * result + ((designation == null) ? 0 : designation.hashCode());
result = prime * result + ((empID == null) ? 0 : empID.hashCode());
result = prime * result + monthlySalary;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (dateOfJoining == null) {
if (other.dateOfJoining != null)
return false;
} else if (!dateOfJoining.equals(other.dateOfJoining))
return false;
if (designation == null) {
if (other.designation != null)
return false;
} else if (!designation.equals(other.designation))
return false;
if (empID == null) {
if (other.empID != null)
return false;
} else if (!empID.equals(other.empID))
return false;
if (monthlySalary != other.monthlySalary)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Employee [name=" + name + ", empID=" + empID + ", designation=" + designation + ", DOJ=" + dateOfJoining
+ ", monthlySalary=" + monthlySalary + "]";
}
}
Ниже приведен класс моего компаратора:
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Employecomparable {
public static void main(String[] args) {
List<Employee> listofemployee = new ArrayList<>();
LocalDate date1 = LocalDate.parse("2018-09-06");
LocalDate date2= LocalDate.of(2011, 12, 12);
listofemployee.add(new Employee("abc", "12345", Designation.ASE,LocalDate.of(2014, 2, 15) , 20000));
listofemployee.add(new Employee("abcd", "24680", Designation.SE, date1, 30000));
listofemployee.add(new Employee("abcde", "13570", Designation.SSE, LocalDate.of(2013, 05, 30), 40000));
listofemployee.add(new Employee("abcdef", "13690", Designation.TL, date2, 60000));
listofemployee.add(new Employee("xyz", "10909", Designation.AM, LocalDate.parse("20/03/2000"), 800000));
listofemployee.add(new Employee("koool", "89076", Designation.M, LocalDate.parse("31/01/2011"), 2000));
Collections.sort(listofemployee, new Comparator<Employee>() {
@Override
public int compare(Employee emp1, Employee emp2) {
if (emp1.getMonthlySalary() > emp2.getMonthlySalary())
return -1;
else if (emp1.getMonthlySalary() < emp2.getMonthlySalary())
return +1;
else
return 0;
}
});
System.out.println(listofemployee);
Collections.sort(listofemployee, new Comparator<Employee>(){
@Override
public int compare(Employee emp1, Employee emp2) {
if(emp1.getDOJ()>emp2.getDOJ())
return +1;
return 0;
}
});
Но при попытке отсортировать список сотрудников по их DOJ я получаю сообщение об ошибке "Оператор> не определен для типа (ов) аргументов: java.time.LocalDate, java.time.LocalDate ".Может кто-нибудь, пожалуйста, помогите мне в этом?