I think that Java have efficient methods for ordering, isn't neccesary to create ordering methods.
If you have method compareTo, you can do a Java collection (for example a list) and use the methods that comes with it.
For example:
import java.util.List;
import java.util.ArrayList;
public class Employee implements Comparable<Employee>{
private int id;
private String name;
private String surname;
private float salary;
public Employee(int id, String name, String surname, float salary) {
super();
this.id = id;
this.name = name;
this.surname = surname;
this.salary = salary;
}
// Compare 2 employees by salary
@Override
public int compareTo(Employee employee) {
// Compared by salary
if (this.salary < employee.salary) {
return -1;
}
else if(this.salary > employee.salary){
return 1;
}
return 0;
}
@Override
public String toString() {
return "Employee [name=" + name + ", salary=" + salary + "]";
}
public static void main(String args[]){
Employee employee1 = new Employee(1, "One", "One Surname", 1800);
Employee employee2 = new Employee(2, "Two", "Two Surname", 200);
Employee employee3 = new Employee(3, "Three", "Three Surname", 1500);
Employee employee4 = new Employee(4, "Four", "Four Surname", 800);
List<Employee> list = new ArrayList<>();
list.add(employee1);
list.add(employee2);
list.add(employee3);
list.add(employee4);
System.out.println(list); // Unordered list
list.sort(null);
System.out.println(list); // Ordered list by salary
}
}
You will have this output:
[Employee [name=One, salary=1800.0], Employee [name=Two, salary=200.0], Employee [name=Three, salary=1500.0], Employee [name=Four, salary=800.0]]
[Employee [name=Two, salary=200.0], Employee [name=Four, salary=800.0], Employee [name=Three, salary=1500.0], Employee [name=One, salary=1800.0]]