java,對象根據屬性排序

來源:互聯網
上載者:User
    /**     * 按照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=wanglei
st.age=41,st.name=lisi
st.age=41,st.name=lisi
st.age=39,st.name=waanglei
/////////////排序之後///////////////
st.age=35,st.name=wanglei
st.age=39,st.name=waanglei
st.age=41,st.name=lisi
st.age=41,st.name=lisi

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.