首页 > 编程 > Java > 正文

java,对象根据属性排序

2019-11-06 06:17:44
字体:
来源:转载
供稿:网友
    /**     * 按照List中的某个Int类型的属性进行排序     *     * @param list     */    @SupPRessWarnings("unchecked")    public static void sortIntMethod(List list) {        Collections.sort(list, new Comparator() {            @Override            public int compare(Object o1, Object o2) {                Student stu1 = (Student) o1;                Student stu2 = (Student) o2;                if (stu1.getAge() > stu2.getAge()) {                    return 1;                } else if (stu1.getAge() == stu2.getAge()) {                    return 0;                } else {                    return -1;                }            }        });    }    //暂没用到    /**     * 按照List中的某个String类型的属性进行排序     *     * @param list     */    @SuppressWarnings("unchecked")    public static void sortByRuleBasedCollator(List list) {        Collections.sort(list, new Comparator() {            @Override            public int compare(Object o1, Object o2) {                return ((java.text.RuleBasedCollator) java.text.Collator.getInstance(java.util.Locale.CHINA)).compare(((Student) o1).getName(), ((Student) o2).getName());            }        });        System.out.println("/////////////排序之后///////////////");        for (int i = 0; i < list.size(); i++) {            Student st = (Student) list.get(i);            System.out.println("st.age=" + st.getAge() + ",st.name=" + st.getName());        }    }
/** * Created by liuyazhou on 2017/3/6. */public class Student {    private int age;    private String name;    private String weight;    public String getWeight() {        return weight;    }    public void setWeight(String weight) {        this.weight = weight;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}
        ArrayList list = new ArrayList();        Student t1 = new Student();        t1.setAge(35);        t1.setName("wanglei");        list.add(t1);        Student t2 = new Student();        t2.setAge(41);        t2.setName("lisi");        list.add(t2);        Student t3 = new Student();        t3.setAge(41);        t3.setName("lisi");        list.add(t3);        Student t4 = new Student();        t4.setAge(39);        t4.setName("waanglei");        list.add(t4);        System.out.println("/////////////排序之前///////////////");        for (int i = 0; i < list.size(); i++) {            Student st = (Student) list.get(i);            System.out.println("st.age=" + st.getAge() + ",st.name=" + st.getName());        }        //按照List中的某个Int类型的属性进行排序        sortIntMethod(list);        System.out.println("/////////////排序之后///////////////");        for (int i = 0; i < list.size(); i++) {            Student st = (Student) list.get(i);            System.out.println("st.age=" + st.getAge() + ",st.name=" + st.getName());        }/////////////排序之前///////////////st.age=35,st.name=wangleist.age=41,st.name=lisist.age=41,st.name=lisist.age=39,st.name=waanglei/////////////排序之后///////////////st.age=35,st.name=wangleist.age=39,st.name=waangleist.age=41,st.name=lisist.age=41,st.name=lisi
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表