Sorting List in Ascending Order | Java Program

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Student implements Comparable {

 private String name;
 private String rollNumber;
 private String department;
// Constructor for the class

public Student(String name,String rollNumber,String department){ = name;
 this.rollNumber = rollNumber;
 this.department = department;
 public String getName() {


 public void setName(String name) { = name;

 public String getRollNumber() {

  return rollNumber;

 public void setRollNumber(String rollNumber) {

  this.rollNumber = rollNumber;

 public String getDepartment() {

  return this.department;

 public void setDepartment(String department) {

  this.department = department;

 //This Function is responsible for sorting.
 public int compareTo(Object student1) {
  if (!(student1 instanceof Student))
   throw new ClassCastException("A Student object expected.");

  int rollNumb = Integer.parseInt(((Student) student1).getRollNumber());
  int hostObjrollNumb = Integer.parseInt(this.getRollNumber());
  return hostObjrollNumb - rollNumb;


 public static void main(String[] args) {
  // TODO Auto-generated method stub

  List lstEstDetailsPrio = new ArrayList();
  //Adds data to the objects
  Student s1 = new Student("Tom","3","CS");
  Student s2 = new Student("Jerry","1","IT");
  Student s3 = new Student("Merry","4","Electronics");
  //Adds objects to the list
  //Sorts the List