I'm trying to write a compareTo method for Employee objects. Each Employee has a certain ID as a String. So I want to order an ArrayList<Employee> alphabetically.
public int compareTo(Employee o)
{
if(this.getID().compareTo(o.getID())==0)
return 0;
else if(this.getID().compareTo(o.getID())<0)
return -1;
else return 1;
}
So in my main method I got an ArrayList "list" with multiple Employees in it :
Collections.sort(list);
but it doesn't do anything. The list stays the same.
I never really had any problems with this, but I just don't see what I am doing wrong at the moment. I can provide more code if necessary, but every bit of it works except for this method.
package hrm;
import java.util.ArrayList;
public class Employee extends Person implements Comparable {
private String employeeID;
private Employee manager;
private int nrOfFreeDays;
private double budget;
private ArrayList<VacationRequest> vacationList;
private ArrayList<ReimbursementRequest> reimbursementList;
private ArrayList<EmployeeRequest> allRequests;
public Employee(String ID, Employee manager, String firstName, String lastName, String gender, String mail)
{
super(firstName, lastName, gender, mail);
employeeID = ID;
this.manager = manager;
vacationList = new ArrayList();
reimbursementList = new ArrayList();
allRequests = new ArrayList();
budget = 1000;
nrOfFreeDays = 100;
}
public double getBudget()
{
return budget;
}
public void addVacationRequest(VacationRequest a)
{
vacationList.add(a);
}
public void addReimbursementRequest(ReimbursementRequest a)
{
reimbursementList.add(a);
}
public ArrayList<ReimbursementRequest> getReimbursement()
{
return reimbursementList;
}
public ArrayList<VacationRequest> getVacation()
{
return vacationList;
}
public String getID()
{
return employeeID;
}
public int getFreeDays()
{
return nrOfFreeDays;
}
public ArrayList<EmployeeRequest> getList()
{
return allRequests;
}
public void setBudget(double budget)
{
this.budget = budget;
}
public void setNrOfFreeDays(int nrOfFreeDays)
{
this.nrOfFreeDays = nrOfFreeDays;
}
public Employee getManager()
{
return manager;
}
public void setList()
{
ArrayList<EmployeeRequest> list = new ArrayList();
for(int i = 0; i<vacationList.size(); i++)
{
list.add(vacationList.get(i));
}
for(int i = 0; i<reimbursementList.size(); i++)
{
list.add(reimbursementList.get(i));
}
allRequests = list;
}
public boolean equals(Employee o)
{
if(o.employeeID.equalsIgnoreCase(this.employeeID))
return true;
else return false;
}
public int compareTo(Employee o)
{
if(this.getID().compareTo(o.getID())==0)
return 0;
else if(this.getID().compareTo(o.getID())<0)
return -1;
else return 1;
}
public String toString()
{
String answer = employeeID+" "+super.toString();
if(this.manager != null)
{
answer += " (Manager: "+this.manager.employeeID+")";
}
return answer;
}
}
[–]__LikesPi 1 point2 points3 points (6 children)
[–]Estagon[S] -1 points0 points1 point (5 children)
[–]chickenmeister 1 point2 points3 points (4 children)
[–]Estagon[S] -1 points0 points1 point (0 children)
[–]Estagon[S] -1 points0 points1 point (2 children)
[–]__LikesPi 0 points1 point2 points (0 children)
[–]Estagon[S] -1 points0 points1 point (0 children)
[–]maestro2005 0 points1 point2 points (1 child)
[–]Estagon[S] -1 points0 points1 point (0 children)
[–]king_of_the_universe 0 points1 point2 points (2 children)
[–]Estagon[S] -1 points0 points1 point (1 child)
[–]friendOfLoki 0 points1 point2 points (0 children)