-3

我的课是这样的:

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

在主要方法中,我创建了 Employee 类的对象并将其保存在 ArrayList 中,例如 objEmpList。现在,我的要求是像 SQL 语句一样对 objEmpList 进行排序,Select * From Employee Order by age,name,empId; Measn,我希望结果按年龄、姓名和 empId 的顺序排序。

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

所以我的结果应该是,

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

我怎样才能达到这个要求?

4

3 回答 3

2

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

这是一个例子:

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),上面的示例对非字符串字段会有些麻烦。但我想我应该把一些乐趣留给你!:)

祝你好运!

于 2012-11-26T07:25:17.177 回答
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();
    }

}
于 2012-11-26T07:42:43.453 回答
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);
于 2012-11-26T07:09:57.480 回答