Java中對單個或多個欄位進行排序

來源:互聯網
上載者:User

標籤:

Java中對單個欄位或多個欄位進行排序

import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;/** * @date: 2015年6月19日 上午10:11:44 */public class CollectionTest {    /**     * 主方法     *      * @param args     *            參數     * @modify by user: {修改人} 2015年6月19日     * @modify by reason:{原因}     */    public static void main(String[] args) {        List<CollectionTest.Man> mans = new ArrayList<CollectionTest.Man>();        CollectionTest collectionTest = new CollectionTest();        Man man = collectionTest.new Man();        man.setParentId(11);        man.setAge(12);        man.setName("某某1_孩子");        Man man2 = collectionTest.new Man();        man2.setParentId(2);        man2.setAge(13);        man2.setName("某某2_孩子");        Man man3 = collectionTest.new Man();        man3.setParentId(2);        man3.setAge(16);        man3.setName("某某1_孩子");        mans.add(man);        mans.add(man2);        mans.add(man3);        sortAge(mans);        System.out.println("*****根據年齡排序********");        System.out.println(mans.toString());        sortParentIdAge(mans);        System.out.println("*****根據父ID,年齡排序********");        System.out.println(mans.toString());    }    /**     * 根據父ID,年齡進行排序     *      * @param mans     *            人列表     * @modify by user: {修改人} 2015年6月19日     * @modify by reason:{原因}     */    private static void sortParentIdAge(List<Man> mans) {        Collections.sort(mans, new Comparator<Man>() {            @Override            public int compare(Man o1, Man o2) {                if (o1.getParentId().equals(o2.getParentId())) {                    return o1.getAge().compareTo(o2.getAge());                } else {                    return o1.getParentId().compareTo(o2.getParentId());                }            }        });    }    /**     * 根據父ID排序     *      * @param mans     * @modify by user: {修改人} 2015年6月19日     * @modify by reason:{原因}     */    private static void sortAge(List<Man> mans) {        Collections.sort(mans, new Comparator<Man>() {            @Override            public int compare(Man o1, Man o2) {                return o1.getAge().compareTo(o2.getAge());            }        });    }    public class Man {                private Integer parentId;        private Integer age;        private String name;        /**         * 擷取parentId         *          * @return parentId parentId         */        public Integer getParentId() {            return parentId;        }        /**         * 設定parentId         *          * @param parentId         *            parentId         */        public void setParentId(Integer parentId) {            this.parentId = parentId;        }        /**         * 擷取age         *          * @return age age         */        public Integer getAge() {            return age;        }        /**         * 設定age         *          * @param age         *            age         */        public void setAge(Integer age) {            this.age = age;        }        /**         * 擷取name         *          * @return name name         */        public String getName() {            return name;        }        /**         * 設定name         *          * @param name         *            name         */        public void setName(String name) {            this.name = name;        }        /**         * @return         * @modify by user: {修改人} 2015年6月19日         * @modify by reason:{原因}         */        @Override        public String toString() {            return "Man [parentId=" + parentId + ", age=" + age + ", name=" + name + "]";        }    }}


Java中對單個或多個欄位進行排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.