import java.io.*; import java.util; //Class Student which implements Comparable interface class Student implements Comparable<Student>{ private Long studentId; private String studentName; private String studentAddress; public Student(Long id, String name, String address){ this.studentId = id; this.studentName = name; this.studentAddress = address; } public Long getStudentId(){ return this.studentId; } public String getStudentName(){ return this.studentName; } public String getStudentAddress(){ return this.studentAddresss; } //Comparing based on studentId @override public int comparaTo(Student s){ return this.studentId - s.studentId; } } public class StudentComparableExample{ public Static void main(String args[]){ ArrayList<Student> studentList = new ArrayList<Student>(); studentList.add(new Student("1", "Ram", "India")); studentList.add(new Student("4", "Shyam", "India")); studentList.add(new Student("3", "Rahul", "India")); studentList.add(new Student("2", "Ravi", "India")); Collections.sort(studentList); System.out.println("Student list after sorting"); for(Student student:studentList){ System.out.println(student.getStudentId() + " " + student.getStudentName() + " " + student.getStudentAddress()); } } }