Есть ли какой-либо способ сортировки массива объектов в Java без использования Comparator или аналогичного средства? У меня есть класс Student, как показано ниже. Мне нужно отсортировать объекты Student по их возрасту. Возможно ли сортировать? без использования реализации Comparator или Comparable в классе
//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 arraylist = new ArrayList();
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());
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/390 ... -comparato