package com.company;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Student {
String name;
int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
class ComparatorImpl implements Comparator {
@Override
public int compare(Student o1, Student o2) {
if(o1.age>o2.age){
return -1;
}
else if (o1.age<o2.age){
return 1;
}
else {
return 0;
}
}
}
class Test{
public static void main(String[] args) {
Student student1 = new Student(“小明”, 18);
Student student2=new Student(“小和”,19);
Student student3=new Student(“小说”,20);
Student[] objects = new Student[3];
objects[0]=student1;
objects[1]=student2;
objects[2]=student3;
Arrays.sort(objects,new ComparatorImpl());
// for (Student student:objects){
// System.out.println(student);
// }
for (int i=0;i<objects.length;i++){
System.out.println(objects[i]);
}
}
}