import java.util.ArrayList;
import java.util.Comparator;
public class Test5 {
public static void main(String[] args) {
Student p1=new Student();
Student p2=new Student();
Student p3=new Student();
p1.age=18;
p2.age=18;
p3.age=15;
ArrayList<Student>list=new ArrayList<Student>();
list.add(p1);
list.add(p2);
list.add(p3);
list.sort(new PersonComparator());
System.out.println(list);
}
}
class Student{
int age;
@Override
public String toString() {
return String.valueOf(age);
}
}
class PersonComparator implements Comparator<Student>{
@Override
public int compare(Student o1, Student o2) {
return o1.age-o2.age;
}
} |