码迷,mamicode.com
首页 > 编程语言 > 详细

List排序

时间:2018-02-06 12:50:56      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:ack   顺序   col   pre   基本   etag   over   name   类型   

<pre name="code" class="java">package cn.edu.zzu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class SortTest {
    
    public static void main(String[] args) {
        List<String> lists = new ArrayList<String>();
        List<A> list = new ArrayList<A>();
        List<B> listB = new ArrayList<B>();
        lists.add("5");
        lists.add("2");
        lists.add("9");
        //lists中的对象String 本身含有compareTo方法,所以可以直接调用sort方法,按自然顺序排序,即升序排序
        Collections.sort(lists);
        
        A aa = new A();
        aa.setName("aa");
        aa.setOrder(1);
        A bb = new A();
        bb.setName("bb");
        bb.setOrder(2);
        list.add(bb);
        list.add(aa);
        //list中的对象A实现Comparable接口
        Collections.sort(list);
        
        B ab = new B();
        ab.setName("ab");
        ab.setOrder("1");
        B ba = new B();
        ba.setName("ba");
        ba.setOrder("2");
        listB.add(ba);
        listB.add(ab);
        //根据Collections.sort重载方法来实现
        Collections.sort(listB,new Comparator<B>(){
            public int compare(B b1, B b2) {
                return b1.getOrder().compareTo(b2.getOrder());
            }
            
        });
        
        System.out.println(lists);
        System.out.println(list);
        System.out.println(listB);
        
    }

}

class A implements Comparable<A>{
    private String name;
    private Integer order;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
    public Integer getOrder() {
        return order;
    }
    public void setOrder(Integer order) {
        this.order = order;
    }
    @Override
    public String toString() {
        return "name is "+name+" order is "+order;
    }
    public int compareTo(A a) {
        return this.order.compareTo(a.getOrder());
    }
    
}

class B{
    private String name;
    private String order;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getOrder() {
        return order;
    }
    public void setOrder(String order) {
        this.order = order;
    }
    @Override
    public String toString() {
        return "name is "+name+" order is "+order;
    }
}</pre><br>
<pre></pre>
<p>运行结果:</p>
<p>[2, 34, 3434]<br>
[name is aa order is 1, name is bb order is 2]<br>
[name is ab order is 1, name is ba order is 2]<br>
</p>
<p></p><pre name="code" class="java">package cn.edu.zzu;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class List元素排序 {
    public static void main(String[] args) {

        List<Student> list = new ArrayList<Student>();

        // 创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中
        Student s1 = new Student();
        s1.setAge(20);
        Student s2 = new Student();
        s2.setAge(19);
        Student s3 = new Student();
        s3.setAge(21);
        list.add(s1);
        list.add(s2);
        list.add(s3);

        System.out.println("排序前:" + list);

        Collections.sort(list, new Comparator<Student>() {

            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型, 返回负数表示:o1 小于o2,
             * 返回0 表示:o1和o2相等, 返回正数表示:o1大于o2。
             */
            public int compare(Student o1, Student o2) {

                // 按照学生的年龄进行升序排列
                if (o1.getAge() > o2.getAge()) {
                    return 1;
                }
                if (o1.getAge() == o2.getAge()) {
                    return 0;
                }
                return -1;
            }
        });
        System.out.println("排序后:" + list);
    }
}

class Student {

    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return getAge() + "";
    }
}</pre><br>
<br>
<p></p>
<p>排序前:[20, 19, 21]<br>
排序后:[19, 20, 21]<br>
</p>
<p><br>
</p>
<p><br>
</p>
<p><br>
</p>
   

 

List排序

标签:ack   顺序   col   pre   基本   etag   over   name   类型   

原文地址:https://www.cnblogs.com/chenglc/p/8421619.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!