美文网首页java基础
Java List的排序

Java List的排序

作者: 韭菜待收割 | 来源:发表于2018-09-20 14:39 被阅读3次

    1、List的排序方式

    1)list中对象实现Comparable接口,重写compareTo接口,排序的字段进行比较。

    //懒,就写一起了
    public class MainTest {
        //测试方法
        public static void main(String[] args) throws Exception {
            List<Person> listA = new ArrayList<Person>();
            listA.add(new Person("name2", 2));
            listA.add(new Person("name1", 1));
            listA.add(new Person("name3", 3));
            //升序
            Collections.sort(listA);
            System.out.println("**********升序**********");
            for (Person p : listA) {
                System.out.println(p);
            }
            //降序
            Collections.reverse(listA);
            System.out.println("**********降序**********");
            for (Person p : listA) {
                System.out.println(p);
            }
    
        }
    
        static class Person implements Comparable<Person> {
    
            private String name;
            private Integer order;
    
            @Override
            public String toString() {
                return "Person{" +
                        "order=" + order +
                        ", name='" + name + '\'' +
                        '}';
            }
    
            public Person(String name, Integer order){
                this.name = name;
                this.order = order;
            }
    
            public Integer getOrder() {
                return order;
            }
    
            @Override
            public int compareTo(Person arg0) {
                //升序
                return this.getOrder().compareTo(arg0.getOrder());
            }
        }
    }
    

    2)重载Collections.sort方法

    //懒,就写一起了
    public class MainTest {
    
        //测试方法
        public static void main(String[] args) throws Exception {
            List<Person> listA = new ArrayList<Person>();
            listA.add(new Person("name2", 2));
            listA.add(new Person("name1", 1));
            listA.add(new Person("name3", 3));
    
            System.out.println("**********升序**********");
            //升序
            Collections.sort(listA, new MySort());
            for (Person person : listA) {
                System.out.println(person);
            }
            //降序
            System.out.println("**********降序**********");
            Comparator result = Collections.reverseOrder(new MySort());
            Collections.sort(listA,result);
            for (Person person : listA) {
                System.out.println(person);
            }
    
            List<Person> listB = new ArrayList<Person>();
            listB.add(new Person("name2", 2));
            listB.add(new Person("name1", 1));
            listB.add(new Person("name3", 3));
            //换一种写法
            Collections.sort(listB, new Comparator<Person>() {
                @Override
                public int compare(Person p1, Person p2) {
                    return p1.getOrder().compareTo(p2.getOrder());
                }
            });
            System.out.println("**换一种写法 升序**********");
            for (Person person : listB) {
                System.out.println(person);
            }
        }
        //比较器类
        static class MySort implements Comparator{
            @Override
            public int compare(Object o1, Object o2){
                Person p1 = (Person)o1;
                Person p2 = (Person)o2;
                //升序
                return p1.getOrder() - p2.getOrder();
            }
        }
        //比较对象
        static class Person {
    
            private String name;
            private Integer order;
    
            @Override
            public String toString() {
                return "Person{" +
                        "order=" + order +
                        ", name='" + name + '\'' +
                        '}';
            }
    
            public Person(String name, Integer order){
                this.name = name;
                this.order = order;
            }
    
            public Integer getOrder() {
                return order;
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:Java List的排序

        本文链接:https://www.haomeiwen.com/subject/cjqnnftx.html