ArrayList排序基于对象属性 [英] ArrayList sorting on the basis of object property

查看:78
本文介绍了ArrayList排序基于对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是Employee bean类。

The following is the Employee bean class.

public class Employee {
    public String name;
    public int age;

    public Employee()
    {

    }

    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;
    }
}



我有其他EmployeeTest类, Employee类的对象,并存储在ArrayList中。

I have other EmployeeTest class and inside it I create the object of the Employee class and store in a ArrayList.

import java.util.ArrayList;

public class EmployeeTest {
    public static void main(String[] args) 
    {
        ArrayList<Employee> empList = new ArrayList<Employee>();
        Employee emp1 = new Employee();
        emp1.setAge(15);
        emp1.setName("Employee1");
        Employee emp2 = new Employee();
        emp2.setAge(10);
        emp2.setName("Employee1");
        empList.add(emp1);
        empList.add(emp2);
        for(Employee emp : empList)
        {
            System.out.println("employee name : " + emp.getName());
            System.out.println("employee age : " + emp.getAge());
        }
    }
}

它是我想根据Employee类age属性排序ArrayList。所以请解释一下如何排序。

Now I have one question regarding it is that I want to sort ArrayList on the basis of Employee class age property. So please explain how can I sort it.

推荐答案

让类实现 Comparable 其他答案,是一个选择。

Letting the class implement the Comparable interface, as suggested in the other answers, is one option.

但一般来说,我建议实施 Comparable 界面,只要该类没有无疑的自然排序 Employee ,肯定有自然排序。

But in general, I'd recommend to NOT implement the Comparable interface, as long as the class does not have an undoubted natural ordering. And for Employee, there is certainly NO natural ordering.

想象一下,你想根据员工的年龄对员工进行排序。一次按升序排列,一次按降序排列。你怎么能这样做?现在想象你想按他们的年龄排序他们一次,按字母顺序排列他们的名字。你不能这样做,而不实现 Comparator 。这是我在这里推荐的:

Imagine you want to sort the Employees according to their age. Once in ascending order, and once in descending order. How could you do that? Now imagine you want to sort them once by their age, and once alphabetically, by their first name. You could not do this, without implementing a Comparator. And that's what I'd recommend here:

您可以创建一个方法

private static Comparator<Employee> byAge()
{
    return new Comparator<Employee>()
    {
        @Override
        public int compare(Employee o1, Employee o2)
        {
            return o1.getAge() - o2.getAge();
        }
    };        
}

然后您可以直接调用

Collections.sort(empList, byAge());

如果要以相反的顺序排序,可以调用

If you want to sort them in reverse order, you can call

Collections.sort(empList, Collections.reverseOrder(byAge()));

如果您想按其名称排序,可以创建一个方法

If you want to sort them by their name, you can create a method

private static Comparator<Employee> byName()
{
    return new Comparator<Employee>()
    {
        @Override
        public int compare(Employee o1, Employee o2)
        {
            return o1.getName().compareTo(o2.getName());
        }
    };        
}

并使用

    Collections.sort(empList, byName());

这比实现

This is much more versatile than implementing Comparable.

这篇关于ArrayList排序基于对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆