ArrayList等常见集合的排序问题

对于ArrayList等常用的集合具体业务类,基本上都实现了Comparable接口,即可以用来比较装载的对象实体。

主要用Collections.sort方法对集合类中的对象进行排序

Collections.sort的两种重载方法

  • Collections.sort(list, comparator)方法,通过comparator规则,实现对list的特定排序。
  • Collections.sort(list),list中的对象自身实现comparator接口

Java集合框架:
comparator

代码示例(演示Collections.sort(list, comparator)方法):

注意:本代码均已在jdk1.6版本下通过测试

  • model,Student类
public class Student {
    private int id;
    private String name;
    private int age;

    /**
     * @Title: Student
     * @Description: TODO
     * @param:
     * @throws
     */
    public Student(int id, String name, int age) {
        // TODO Auto-generated constructor stub
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    public int getId() {
        return id;
    }

    @Override
    public String toString() {
        return String.format("Student [age=%s, name=%s, id=%s]", age, name, id);
    }
}
  • 测试类
public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<Student> arrayList = new ArrayList<Student>();
        Student s1 = new Student(1, "jack", 20);
        Student s2 = new Student(2, "jack", 20);
        Student s3 = new Student(3, "lily", 29);
        Student s4 = new Student(4, "tom", 30);
        Student s5 = new Student(5, "rose", 31);
        Student s6 = new Student(6, "crane", 20);
        Student s7 = new Student(7, "jack", 25);
        Student s8 = new Student(8, "rose", 27);
        Student s9 = new Student(9, "lucy", 18);

        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        arrayList.add(s4);
        arrayList.add(s5);
        arrayList.add(s6);
        arrayList.add(s7);
        arrayList.add(s8);
        arrayList.add(s9);
        Comparator<Student> studentComparator = new Comparator<Student>() {

            /**
             * 
             * @Title: compare
             * @Description: 先比较age,再比较name,最后比较id
             * @param: @param o1
             * @param: @param o2
             * @param: @return
             * @return: int
             * @throws
             */
            @Override
            public int compare(Student o1, Student o2) {
                // TODO Auto-generated method stub
                if (o1.getAge() != o2.getAge()) {
                    return o1.getAge() - o2.getAge();
                } else if (!o1.getName().equals(o2.getName())) {
                    return o1.getName().compareTo(o2.getName());
                } else if (o1.getId() != o2.getId()) {
                    return o1.getId() - o2.getId();
                }
                return 0;
            }
        };
        Collections.sort(arrayList, studentComparator);

        for (Student student : arrayList) {
            System.out.println(student.toString());
        }
    }
  • 测试结果
Student [age=18, name=lucy, id=9]
Student [age=20, name=crane, id=6]
Student [age=20, name=jack, id=1]
Student [age=20, name=jack, id=2]
Student [age=25, name=jack, id=7]
Student [age=27, name=rose, id=8]
Student [age=29, name=lily, id=3]
Student [age=30, name=tom, id=4]
Student [age=31, name=rose, id=5]
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值