Is there any way sorting an arraylist of objects in java without using Comparator or comparable ,I have Student class as shown below i need to Sort the Students Objects based on their Age., Is it possible to Sort ? without using implementing Comparator or Comparable in class
//Class of Students
//comparable or comparator Not implemented
public class Student {
private String studentname;
private int rollno;
private int studentage;
public Student(int rollno, String studentname, int studentage) {
this.rollno = rollno;
this.studentname = studentname;
this.studentage = studentage;
}
public String getStudentname() {
return studentname;
}
public void setStudentname(String studentname) {
this.studentname = studentname;
}
public int getRollno() {
return rollno;
}
public void setRollno(int rollno) {
this.rollno = rollno;
}
public int getStudentage() {
return studentage;
}
public void setStudentage(int studentage) {
this.studentage = studentage;
}
}
import java.util.*;
public class ArrayListSorting {
public static void main(String args[]){
//Array of Student Objects
ArrayList<Student> arraylist = new ArrayList<Student>();
arraylist.add(new Student(223, "Chaitanya", 26));
arraylist.add(new Student(245, "Rahul", 24));
arraylist.add(new Student(209, "Ajeet", 32));
Collections.sort(arraylist);
for(Student str: arraylist){
System.out.println(str.getStudentage());
}
}
}