Query on compareTo method
Following is the program to sort the employees on the Name basis
import java.util.*;
class EmployeeSortTest
{
public static void main ( String[] tam )
{
// Array of Employees
Employee[] a = new Employee[3];
a[0] = new Employee("Tam",350000);
a[1] = new Employee("Dravid",500000);
a[2] = new Employee("Sachin",700000);
// Sorting the Employees with name basis using Arrays utility class
Arrays.sort(a);
for( Employee x : a)
System.out.println(" Name = " + x.getName() +
"Salary = " + x.getSalary());
}
}
class Employee implements Comparable
{
public Employee(String n, double s )
{
name = n;
salary = s;
}
public int compareTo(Employee other)
{
return name.compareTo(other.name);
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
private double salary;
private String name;
}
My problem is I also want to sort the Employees on the Salary basis without modifying this existing code.
i.e to have an another version of compareTo() method in this class that implements Comparable interface
which sort on salary basis
public int compareTo(Employee other)
{
if ( salary > other.salary) return 1;
if ( salary < other.salary) retrurn -1;
return 0;
}
But compiler reports that compareTo() is already defined in Employee
Pls give solution to this

