Program to sort Employee list on basis of name in descending order by implementing Comparable interface and overriding its compareTo method

import java.util.ArrayList;
import java.util.Collections;
class Employee implements Comparable<Employee>{
   String name;
   String id;
   public Employee(String name, String id) { = name; = id;
   public int compareTo(Employee otherEmployee) {
          // sort Employee on basis of name(descending order)

   public String toString() {
       return "Employee{" + "name=" + name + ", id=" + id  + '}';
* @author AnkitMittal
* Copyright (c), AnkitMittal
* Main class
public class ComparableUsage {
   public static void main(String[] args) {
       Employee emp1=new Employee("sam","4");
       Employee emp2=new Employee("amy","2");
       Employee emp3=new Employee("brad","1");

       ArrayList<Employee> list=new ArrayList<Employee>();
       System.out.println("list Before sorting : \n"+list);
       System.out.println("\nlist after sorting on basis of name(descending order) : \n"+list);
list Before sorting :
[Employee{name=sam, id=4}, Employee{name=amy, id=2}, Employee{name=brad, id=1}]
list after sorting on basis of name(descending order) :
[Employee{name=sam, id=4}, Employee{name=brad, id=1}, Employee{name=amy, id=2}]

Must read for you :