Reputation: 189
Im a beginner on Java and I'm having problem using Comparable to sort Employee objects by name. I've been readin the Java documentation with no much luck. I'm looking for some guidance to set me on the right track.
public class Employee implements Comparable<Employee> {
private String name ="No name yet";
Task task;
public Employee(String setName) {
name = setName;
}
...
public int compareTo(Employee object) {
Employee emp = (Employee) object;
return this.name.compareTo(emp.name);
}
Im trying to call the compare method like this, but im not sure this is right.
public static void main(String[] args){
Employee[] emp = new Employee[4];
emp[0] = new HourlyEmp("Green", 22.50, 40);
emp[1] = new ExemptEmp("White", 1000.00);
emp[2] = new ExemptEmp("Black", 1100.00);
emp[3] = new ExemptEmp("Brown", 1346.15);
Array.sort(emp); <<--ERROR: The method sort(Employee[]) is undefined
for the type Array
printReport("Employee list sorted on names", emp);
Any help would be greatly appreciated.Thanks!
Upvotes: 1
Views: 2938
Reputation: 328873
It is Arrays.sort(emp);
, with an s
at Arrays
. This will call the Arrays#sort
method.
Upvotes: 2