java:Object对象进行排序
package arrays.compara;
import java.util.Arrays;
public class Student {
public static void main(String[] args) {
Stu[] stus = new Stu[]{
new Stu(156,34,"ad"),
new Stu(153,24,"cc"),
new Stu(126,37,"ab"),
new Stu(176,45,"as"),
new Stu(156,34,"bd")
};
Arrays.sort(stus);
for (int i = 0; i < stus.length; i++) {
System.out.println(stus[i].toString());
}
}
}
class Stu implements Comparable<Object> {
int hight;
int age;
String name;
public Stu(int hight, int age, String name) {
this.hight = hight;
this.age = age;
this.name = name;
}
//年龄比较
/*public int compareTo(Object obj) {
return (this.age > ((Stu)obj).age) ? 1 : (this.age == ((Stu)obj).age) ? 0 : -1;
}*/
//身高比较
/*public int compareTo(Object obj) {
return (this.hight > ((Stu)obj).hight) ? 1 : (this.hight == ((Stu)obj).hight) ? 0 : -1;
}*/
//姓名比较
public int compareTo(Object obj) {
return this.name.compareTo(((Stu)obj).name);
}
//重写toString()方法
@Override
public String toString() {
return this.hight+"-->"+this.age+"-->"+this.name;
}
}