package cn.Home;
import java.util.Arrays;
import java.util.Comparator;
import org.junit.Test;
public class Demo03 {
@Test
public void test1() {
// 默认所有系统的类Int,Integer,double ,float ,String可以排序
Stud[] studs = new Stud[] { new Stud("Jerry",21), new Stud("Jerry",20),
new Stud("Alex", 9), new Stud("Jack",23),new Stud("Jack",16) };
Arrays.sort(studs, new Comparator() {
@Override
public int compare(Stud o1, Stud o2) {
//**************************************************
//如果姓名相同,则用年龄进行比较;
if (o1.getName().equals(o2.getName())){
return -1;
}
//**************************************************
return o1.getName().compareTo(o2.getName());
}
});
System.err.println(Arrays.toString(studs));
}
}
class Stud {
private String name;
private int age;
public Stud(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Stud [name=" + name + ", age=" + age + "]";
}
}