利用Arrays.sort()对对象进行按某些属性排序,两种实现方式,内部比较器(comparable)和外部比较器(comparator)。
1.内外比较器的区别
同:都是接口类型,实现对象的排序
compareTo(Object o) : 本类的属性>传入对象属性,返回正数,为升序排列;反之为降序
compare(Object o1,Object o2):第一个参数对象(o1)的属性 > 第二个参数对象(o2)的属性,为升序排列;反之为降序
异:1.comparable 在java.lang 包中,而comparator 在java.util包中
2.实现comparable接口的方法为compareTo(Object o),而实现comparator的方法为 compare(Object o1,Object o2)
3.Comparator 使用灵活,不需要修改源码.但是,使用时需要传入比较器对象;Comparable使用简单,但是需要修改源码
2.实现案例
分别利用内部、外部比较器对Score、Stu类的对象进行排序
import java.util.Arrays;
import java.util.Comparator;
/**
* 按指定对象的某个属性排序
* @author lyf3312
*
*/
public class ObjectSort {
//测试排序结果
public static void main(String[] args) {
/*
//内部比较器
Score s1 = new Score(90, 80, 110);
Score s2 = new Score(91, 90, 98);
Score s3 = new Score(90, 88, 80);
Score s4 = new Score(97, 86, 100);
Score[] s = {s1,s2,s3,s4};
// Arrays.sort(s); //语文成绩升序排列
Arrays.sort(s); //语文成绩升序排列,语文成绩相同,按数学成绩降序排列
for(Score ss :s) {
ss.show();
}
*/ //外部比较器
Stu stu1 = new Stu("lee", 20, 70.22);
Stu stu2 = new Stu("ggg", 20, 50.22);
Stu stu3 = new Stu("json", 28, 60.69);
Stu stu4 = new Stu("python", 32, 88.66);
Stu[] stu = {stu1,stu2,stu3,stu4};
// Arrays.sort(stu,stu1); // 按年龄升序排序
Arrays.sort(stu,new StuCompare()); // 按年龄升序排序,年龄相同按体重升序
for(Stu st : stu) {
st.show();
}
}
}
/**
* 待排序对象,利用内部比较器进行排序
* @author Administrator
*
*/
class Score implements Comparable<Score>{
public int math;
public int chinese;
public int english;
public Score(int chinese, int math , int english) {
super();
this.math = math;
this.chinese = chinese;
this.english = english;
}
public Score() {
super();
// TODO Auto-generated constructor stub
}
public void show() {
System.out.print("chinese:"+chinese+"\tmath"+math+"\tenglish:"+english+"\n");
}
//按单一属性排列
/*
@Override
public int compareTo(Score o) {
// TODO Auto-generated method stub
if (this.chinese > o.chinese) {
return 1;
}else if(this.chinese < o.chinese) {
return -1;
}else {
return 0;
}
}
*/
//按多个属性排列
@Override
public int compareTo(Score o) {
// TODO Auto-generated method stub
if (this.chinese > o.chinese) {
return 1;
}else if(this.chinese < o.chinese) {
return -1;
}else {
//语文成绩相同,按数学成绩降序排列
if(this.math >o.math) {
return -1;
}else if(this.math < o.math) {
return 1;
}else {
return 0;
}
}
}
}
/**
* 待排序对象,利用外部比较器进行排序
* @author Administrator
*
*/
class Stu{
public String name;
public int age;
public double weight;
public Stu(String name, int age, double weight) {
super();
this.name = name;
this.age = age;
this.weight = weight;
}
public Stu() {
super();
// TODO Auto-generated constructor stub
}
public void show() {
System.out.println("name:"+name+"\tage:"+age+"\tweight:"+weight+"\n");
}
}
//stu的外部比较器
class StuCompare implements Comparator<Stu>{
//按单一属性排序
/*
@Override
public int compare(Stu o1, Stu o2) {
// TODO Auto-generated method stub
if(o1.age > o2.age) {
return 1;
}else if(o1.age < o2.age) {
return -1;
}else {
return 0;
}
}
*/
//按多个属性排序
@Override
public int compare(Stu o1, Stu o2) {
// TODO Auto-generated method stub
if(o1.age > o2.age) {
return 1;
}else if(o1.age < o2.age) {
return -1;
}else {
if(o1.weight > o2.weight) {
return 1;
}else if(o1.weight < o2.weight) {
return -1;
}else {
return 0;
}
}
}
}