1

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!

2 Answers 2

2

Try Arrays.sort(emp); instead.

Sign up to request clarification or add additional context in comments.

Comments

2

It is Arrays.sort(emp);, with an s at Arrays. This will call the Arrays#sort method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.