首页 > 其他 > 详细

先按成绩由高到低,相等则按年龄由低到高

时间:2014-08-14 16:16:18      阅读:227      评论:0      收藏:0      [点我收藏+]

class Studentx implements Comparable<Studentx> {
    private String name;
    private int age;
    private float score;

    public Studentx(String name, int age, float score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String toString() {
        return "Studentx [name=" + name + ", age=" + age + ", score=" + score
                + "]";
    }

    public int compareTo(Studentx o) {
        if (this.score > o.score) {
            return -1;
        } else if (this.score < o.score) {
            return 1;
        } else {
            if (this.age > o.age) {
                return 1;
            } else {
                return 0;
            }
        }
    }

}

public class ComparableDemo01 {
    public static void main(String[] args) {
        Studentx studentx[] = { new Studentx("von", 20, 90.0f),
                new Studentx("korea", 24, 92.0f),
                new Studentx("susan", 18, 90.0f),
                new Studentx("lily", 30, 92.0f),
                new Studentx("boy", 28, 84.2f), new Studentx("cais", 23, 97.3f) };
        java.util.Arrays.sort(studentx);
        for (int i = 0; i < studentx.length; i++) {
            System.out.println(studentx[i]);
        }
    }
}

先按成绩由高到低,相等则按年龄由低到高,布布扣,bubuko.com

先按成绩由高到低,相等则按年龄由低到高

原文:http://www.cnblogs.com/vonk/p/3912473.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!