sort Employee on basis of Name, Salary and joining Date






In this core java programming tutorial we will write a program to sort Employee on basis of Name, Salary and joining Date.



Program 1 / example to sort Employee on basis of Name, Salary and joining Date >

/*
* sort Employee on basis of Name, Salary and joining Date
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
//Implement Comparable to sort Employee on basis of Name, Salary and joining Date
class Employee implements Comparable<Employee> {
   String name;
   Integer salary;
   Date JoiningDate;
   public Employee() {
   }
   public Employee(String n, Integer f, Date d) {
          name = n;
          salary = f;
          JoiningDate = d;
   }
   public String toString() {
          return "\n name=" + name + ",salary=" + salary + ",JoiningDate="
                       + JoiningDate;
   }
   public int compareTo(Employee o) {
          return this.name.compareTo(o.name) + (this.salary.compareTo(o.salary))
                       + (this.JoiningDate.compareTo(o.JoiningDate));
   }
}
public class SortEmployeeOnBasisOfNameSalaryDate {
   public static void main(String[] args) {
          Employee emp1 = new Employee("ank", 2000, new Date(2016 - 1900, 11, 14));
          Employee emp2 = new Employee("dav", 500, new Date(2016 - 1900, 11, 23));
          Employee emp3 = new Employee("ank", 1000, new Date(2016 - 1900, 11, 22));
          Employee emp4 = new Employee("sam", 9000, new Date(2016 - 1900, 11, 29));
          Employee emp5 = new Employee("ank", 1000, new Date(2016 - 1900, 11, 19));
          List<Employee> l = new ArrayList<Employee>();
          l.add(emp1);
          l.add(emp2);
          l.add(emp3);
          l.add(emp4);
          l.add(emp5);
          Collections.sort(l); // SORT
          System.out.println(l); // Display list
   }
}
/*OUTPUT
[
name=ank,salary=2000,JoiningDate=Wed Dec 14 00:00:00 IST 2016,
name=ank,salary=1000,JoiningDate=Mon Dec 19 00:00:00 IST 2016,
name=ank,salary=1000,JoiningDate=Thu Dec 22 00:00:00 IST 2016,
name=dav,salary=500,JoiningDate=Fri Dec 23 00:00:00 IST 2016,
name=sam,salary=9000,JoiningDate=Thu Dec 29 00:00:00 IST 2016]
*/


In this core java programming tutorial we wrote a program to sort Employee on basis of Name, Salary and joining Date.


Program 2 / example to sort Employee in reverse order on basis of Name, Salary and joining Date >
/*
* sort Employee1 in reverse order on basis of Name, Salary and joining Date
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
//Implement Comparable to sort Employee1 in reverse order on basis of Name, Salary and joining Date
class Employee1 implements Comparable<Employee1> {
   String name;
   Integer salary;
   Date JoiningDate;
   public Employee1() {
   }
   public Employee1(String n, Integer f, Date d) {
          name = n;
          salary = f;
          JoiningDate = d;
   }
   public String toString() {
          return "\n name=" + name + ",salary=" + salary + ",JoiningDate="
                       + JoiningDate;
   }
   public int compareTo(Employee1 o) {
          return o.name.compareTo(this.name) + (o.salary.compareTo(this.salary))
                       + (o.JoiningDate.compareTo(this.JoiningDate));
   }
}
public class SortEmployeeReverseOrderOnBasisOfNameSalaryDate {
   public static void main(String[] args) {
          Employee1 emp1 = new Employee1("ank", 2000, new Date(2016 - 1900, 11, 14));
          Employee1 emp2 = new Employee1("dav", 500, new Date(2016 - 1900, 11, 23));
          Employee1 emp3 = new Employee1("ank", 1000, new Date(2016 - 1900, 11, 22));
          Employee1 emp4 = new Employee1("sam", 9000, new Date(2016 - 1900, 11, 29));
          Employee1 emp5 = new Employee1("ank", 1000, new Date(2016 - 1900, 11, 19));
          List<Employee1> l = new ArrayList<Employee1>();
          l.add(emp1);
          l.add(emp2);
          l.add(emp3);
          l.add(emp4);
          l.add(emp5);
          Collections.sort(l); // SORT
          System.out.println(l); // Display list
   }
}
/*OUTPUT
[
name=sam,salary=9000,JoiningDate=Thu Dec 29 00:00:00 IST 2016,
name=dav,salary=500,JoiningDate=Fri Dec 23 00:00:00 IST 2016,
name=ank,salary=2000,JoiningDate=Wed Dec 14 00:00:00 IST 2016,
name=ank,salary=1000,JoiningDate=Thu Dec 22 00:00:00 IST 2016,
name=ank,salary=1000,JoiningDate=Mon Dec 19 00:00:00 IST 2016]
*/



So in this core java programming tutorial we wrote a program to sort Employee on basis of Name, Salary and joining Date.




Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.



Related >








Level2 programs


















No comments:

Post a Comment