0 votes
1.8k views
in Java by
I want to know about comparable example to sort eithor accending order or descending order.

1 Answer

0 votes
by
package com;
 
import java.util.Date;
 
public class Employee implements Comparable<Employee>{
private String name;
private int salary;
private Date yearOfJoining;
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getSalary() {
return salary;
}
 
public void setSalary(int salary) {
this.salary = salary;
}
 
public Date getYearOfJoining() {
return yearOfJoining;
}
 
public void setYearOfJoining(Date yearOfJoining) {
this.yearOfJoining = yearOfJoining;
}
 
@Override
public int compareTo(Employee obj) {
//Accending order
return this.getSalary() - ((Employee)obj).getSalary();
//Decending order
// return ((Employee)obj).getSalary() - this.getSalary();
}
}
 
package com;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
 
public class ComparableTestMain {
 
/**
* @param args
*/
public static void main(String[] args) {
List<Employee> emps = new ArrayList<Employee>();
addEmployees(emps);
System.out.println("******************** Accending order Sorting *****************");
System.out.println("########## Before Sorting #####################");
for(int num = 0; num < emps.size(); num++){
Employee emp = emps.get(num);
System.out.println("Name: " + emp.getName() + " Salary: " + emp.getSalary() + " YearOfJoining: " + emp.getYearOfJoining());
}
Collections.sort(emps);
System.out.println("########## After Sorting #####################");
for(int num = 0; num < emps.size(); num++){
Employee emp = emps.get(num);
System.out.println("Name: " + emp.getName() + " Salary: " + emp.getSalary() + " YearOfJoining: " + emp.getYearOfJoining());
}
}
 
private static void addEmployees(List<Employee> emps) {
Employee emp = new Employee();
emp.setName("Raj");
emp.setSalary(1000);
emp.setYearOfJoining(new Date());
emps.add(emp);
Employee emp1 = new Employee();
emp1.setName("Mohit");
emp1.setSalary(2000);
emp1.setYearOfJoining(new Date());
emps.add(emp1);
Employee emp2 = new Employee();
emp2.setName("Rinku");
emp2.setSalary(1000);
emp2.setYearOfJoining(new Date());
emps.add(emp2);
}
 
}
 
Output: 
******************** Accending order Sorting *****************
########## Before Sorting #####################
Name: Raj Salary: 1000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
Name: Mohit Salary: 2000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
Name: Rinku Salary: 1000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
########## After Sorting #####################
Name: Raj Salary: 1000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
Name: Rinku Salary: 1000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
Name: Mohit Salary: 2000 YearOfJoining: Thu Sep 05 12:15:43 IST 2013
 
******************** Descending order Sorting *****************
########## Before Sorting #####################
Name: Raj Salary: 1000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
Name: Mohit Salary: 2000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
Name: Rinku Salary: 1000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
########## After Sorting #####################
Name: Mohit Salary: 2000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
Name: Raj Salary: 1000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
Name: Rinku Salary: 1000 YearOfJoining: Thu Sep 05 12:23:11 IST 2013
 

 

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...