使用不同属性的组合对ArrayList的对象进行排序

时间:2012-11-26 07:03:25

标签: java collections arraylist comparator comparable

我的班级是这样的:

class Employee{
  int age;
  String name;
  int empId;
  .
  .
  . 
 }

在main方法中,我创建了Employee类的对象,并将其保存在ArrayList中,称为objEmpList。 现在,我的要求是像SQL Statemnt一样对objEmpList进行排序,   按年龄,名称,empId选择*来自员工订单; 测量,我希望结果按年龄,名称和顺序排序。 EMPID。​​

例如:   我的数据如下:     10 Nirdesh 1     10 Ambesh 222      9拉梅什9     12 Amar 3

所以我的结果应该是,

 9    Ramesh  9
 10   Ambesh  222
 10   Ramesh  1 
 12   Amar    3

我如何达到这个要求?

3 个答案:

答案 0 :(得分:2)

您应该实现一个实现Comparator的新类EmployeeComparator。通过指定字段名称public EmployeeComparator(String... fields)的vararg列表来配置字段的顺序。

以下是一个例子:

public class CarComparator implements Comparator<Car> {

private final List<String> fieldSortOrder;

public CarComparator(String... fieldSortOrder) {
    this.fieldSortOrder = new ArrayList<String>(
            Arrays.asList(fieldSortOrder));
}

@Override
public int compare(Car a, Car b) {
    try {
        return cmp(a, b, fieldSortOrder);
    } catch (Exception e) {
        return 0;
    }
}

private int cmp(Car a, Car b, final List<String> fields) throws Exception {
    if (fields.isEmpty())
        return 0;

    PropertyDescriptor pd = new PropertyDescriptor(fields.get(0), Car.class);
    String ma = (String) pd.getReadMethod().invoke(a);
    String mb = (String) pd.getReadMethod().invoke(b);
    if (ma.compareTo(mb) == 0) {
        return cmp(a, b, fields.subList(1, fields.size()));
    } else {
        return ma.compareTo(mb);
    }
}

}

然后将列表排序如下:

Collection.sort(cars, new CarComparator("brand", "mileage"));

您需要为值对象中的每个字段设置访问器(即getter和setter),上面的示例对非字符串字段会有一些麻烦。但我想我应该为你留下一些乐趣! :)

祝你好运!

答案 1 :(得分:2)

- 如果您希望根据对象的多个属性进行排序,则可以使用java.util.Compartor<T>界面。

- 您必须使用Collections.sort(List<?> l, Comparator c)

<强>例如

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

class Car {

    private String name;
    private String brand;
    private double cost;

    public Car(String name, String brand, double cost) {

        this.name = name;
        this.brand = brand;
        this.cost = cost;
    }

    public String getName() {
        return name;
    }

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

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }

    public String toString() {

        return getName();
    }

}

public class Hog {

    ArrayList<Car> cars = new ArrayList<Car>();

    public void setIt() {

        cars.add(new Car("Padmini", "Fiat", 100008.00));
        cars.add(new Car("XYlo", "Mahindra", 100000.00));
        cars.add(new Car("Swift", "Maruti", 200000.00));
    }

    public void sortIt() {

        Collections.sort(cars, new NameComparator());
        System.out.println(cars);
        Collections.sort(cars, new BrandComparator());
        System.out.println(cars);
        Collections.sort(cars, new CostComparator());
        System.out.println(cars);
    }

    class NameComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return c1.getName().compareTo(c2.getName());
        }
    }

    class BrandComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return c1.getBrand().compareTo(c2.getBrand());
        }
    }

    class CostComparator implements Comparator<Car> {

        public int compare(Car c1, Car c2) {

            return new Double(c1.getCost()).compareTo(new Double(c2.getCost()));
        }
    }

    public static void main(String[] args) {

        Hog h = new Hog();

        h.setIt();
        h.sortIt();
    }

}

答案 2 :(得分:0)

使Employee实现Comparable,然后调用Collections.sort(list);

public class Employee implements Comparable<Employee> {

    // rest of class omitted

    public int compareTo(Employee e) {
       // Here's how you order by age, name, empId
       if (age != e.age) return age - e.age;
       if (!name.equal(e.name) return name.compareTo(e.name);
       return empId - e.empId;
    }
}

然后

Collections.sort(list);