import java.util.*;
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
public class Main {
public static void main(String[] args) {
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("Alice", 20));
studentList.add(new Student("Bob", 18));
studentList.add(new Student("Charlie", 20));
Collections.sort(studentList, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int ageComparison = Integer.compare(s1.getAge(), s2.getAge());
if (ageComparison != 0) {
return ageComparison;
} else {
return s1.getName().compareTo(s2.getName());
}
}
});
for (Student student : studentList) {
System.out.println("Name: " + student.getName() + ", Age: " + student.getAge());
}
}
}