Problem : Create a standalone jar executable which will print the list of candidates appearing for the interview sorted order by name, age and experience ascending.
I am having trouble figuring out the compareTo method logic to be able to sort the 3 fields in the given problem.
Employee Class
package com.example.demo.employee;
public class Employee implements Comparable<Employee> {
private String name;
private int age;
private int exp;
public Employee(String name, int age, int exp) {
super();
this.name = name;
this.age = age;
this.exp = exp;
}
public Employee() {
}
// getter setter
@Override
public int compareTo(Employee emp) {
// I do not think this logic is correct
// I have read the other stack overflow posts with similar problem
// but failing to under stand what to do in this method.
int result = (this.name).compareTo(emp.name);
if ( result == 0 ) {
result = (this.age).compareTo(emp.age);
}
if ( result == 0 ) {
result = (this.exp).compareTo(emp.exp);
}
return result;
}
}
Employee Service Class
package com.example.demo.employee;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class EmployeeService {
public List<Employee> getEmployees() {
Employee e1 = new Employee("Sandhya", 20, 0);
Employee e2 = new Employee("Kemp", 24, 2);
Employee e3 = new Employee("Anil", 22, 3);
Employee e4 = new Employee("Kumar", 30, 6);
Employee e5 = new Employee("Tim", 32, 7);
public List<Employee> getEmployees() {
List<Employee> eList = new ArrayList<>();
eList.add(e1);
eList.add(e2);
eList.add(e3);
eList.add(e4);
eList.add(e5);
Collections.sort(eList);
return eList;
}
}
EmployeeController
package com.example.demo.employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class EmployeeController {
@Autowired
EmployeeService es;
@RequestMapping(value = "/")
public List<Employee> getEmpList(){
List<Employee> list = es.getEmployees();
return list;
}
}
No need to implement Comparable
and override the compareTo
method, just use Comparator
Comparator<Employee> c = Comparator.comparing(Employee::getName)
.thenComparing(Employee::getAge)
.thenComparing(Employee::getExp);
And the use Collections.sort()
to sort the list using passed Comparator
Collections.sort(eList,c);
By using Comparable
The problem is age
and exp
are int
type which is primitive were you cannot use compareTo
method, change their type to Integer
wrapper object or use Integer.compare(int a, int b)
method
private int age; // to private Integer age
private int exp; // to private Integer exp
So that you can use compareTo
on age
and exp
this.getAge().compareTo(o.getAge());
this.getExp().compareTo(o.getExp());
If not look at my solution below using the Integer.compare(int a, int b)
solution
@Override
public int compareTo(Employee o) {
int result = this.getName().compareTo(o.getName());
if (result == 0) {
result = Integer.compare(this.getAge(), o.getAge());
if (result == 0) {
return Integer.compare(this.getExp(), o.getExp());
}
return result;
}
return result;
}