title: compareTo method logic to sort List by multiple variables
body: <p>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.</p>
  
  <p>I am having trouble figuring out the compareTo method logic to be able to sort the 3 fields in the given problem.</p>
  
  <p>Employee Class</p>
  
  <pre><code>package com.example.demo.employee;
  
  public class Employee implements Comparable&lt;Employee&gt; {
  
  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;
   }
  
  }
  </code></pre>
  
  <p>Employee Service Class</p>
  
  <pre><code>package com.example.demo.employee;
  
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  @Service
  public class EmployeeService {
  
  
  public List&lt;Employee&gt; 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&lt;Employee&gt; getEmployees() {
  
      List&lt;Employee&gt; eList = new ArrayList&lt;&gt;();
      eList.add(e1);
      eList.add(e2);
      eList.add(e3);
      eList.add(e4);
      eList.add(e5);
  
      Collections.sort(eList);
  
      return eList;
    }
  }
  </code></pre>
  
  <p>EmployeeController</p>
  
  <pre><code>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&lt;Employee&gt; getEmpList(){
      List&lt;Employee&gt; list = es.getEmployees();
      return list;
    }
  
  }
  </code></pre>