在集合中进行排序比较/可比较

时间:2014-09-05 18:39:25

标签: java sorting collections comparator comparable

我试图按名称按字母顺序对Person进行排序,然后按年龄按升序排序

我可以按名称的相反顺序排序,但无法按年龄的升序排序。

PerId Name Age
--------------
7   Simpson 8
3   Michel  10
9   Mark    35
2   Mark    30
8   Lee     40
1   Jorge   19
5   Frank   28
6   Bill    34
4   Bill    16

我的代码

class Util {
    public static List<Person> getPersons() { 
        List<Person> col = new ArrayList<Person>(); 
        col.add(new Person(5, "Frank", 28)); 
        col.add(new Person(1, "Jorge", 19)); 
        col.add(new Person(6, "Bill", 34)); 
        col.add(new Person(3, "Michel", 10)); 
        col.add(new Person(7, "Simpson", 8)); 
        col.add(new Person(4, "Bill",16 )); 
        col.add(new Person(8, "Lee", 40)); 
        col.add(new Person(9, "Mark", 35));
        col.add(new Person(2, "Mark", 30)); 
        return col; 
    } 
}

class Person implements Comparator<Person> { 
    private int perId; 
    private String name; 
    private int age;

    public Person(){}

    public Person(int perId, String name, int age) {
        this.perId = perId;
        this.name = name;
        this.age = age;
    }

    //.... getters/setters

    @Override
    public int compare(Person e1, Person e2) {
        return e2.getName().compareTo(e1.getName());
    }
}

class PersonId implements Comparable<Person>{
    private Integer perId;

    @Override
    public int compareTo(Person o) {
        return this.perId.compareTo(o.getPerId());
    }
    //...getter
}

public class TestPersonSort {
    public static void main(String[] args) {
        List<Person> coll = Util.getPersons(); 
        Collections.sort(coll, new Person()); 
        // sort method 
        printList(coll);

        //problem here ***********************************************
        //Collections.sort(coll); 
    } 
    private static void printList(List<Person> list) { 
        System.out.println("PerId\tName\tAge"); 
        for (Person e: list) { 
            System.out.println(e.getPerId() + "\t" + e.getName() + "\t" + e.getAge()); 
        }
    }
}

2 个答案:

答案 0 :(得分:2)

Comparator课程首先比较这个人的姓名。如果比较不是0,请将其返回。如果是0,则继续比较此人的年龄。

public int compare(Person e1, Person e2) {
    int comp = e2.getName().compareTo(e1.getName());  // desc
    if (comp != 0) return comp;
    return e1.getAge() - e2.getAge();  // asc
}

另一个更复杂但更灵活的选择是拥有一个使用其他Comparator的{​​{1}}。然后,您可以提供Comparator来比较Comparator<Person>中的每个属性,以组合它们的效果。

List

答案 1 :(得分:0)

一种简单的方法是Person实施Comparable<Person>。然后,main()可以使用Collections.sort(coll);

class Person implements Comparable<Person> { 
    private int perId; 
    private String name; 
    private int age;

    public Person(){}

    public Person(int perId, String name, int age) {
        this.perId = perId;
        this.name = name;
        this.age = age;
    }

    //.... getters/setters

    @Override
    public int compareTo(Person other) {
        int result;

        if (other == null)
            return(0);

        if (other == this)
            return(0);

        result = name.compareTo(other.name);

        if (result != 0)
            return(-result);    // Sort names in reverse

        return(age - other.age);   // Sort age ascending
    }
}
相关问题