簡體   English   中英

在集合中比較/比較

[英]Sorting in Collections Comparator/Comparable

我正在嘗試按名稱的字母反向排列人,然后按年齡升序排列

我可以按名稱的相反順序排序,但是無法按年齡的升序再次排序。

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

首先讓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使用其他Comparator 然后,您可以提供Comparator<Person> ,它們在List中比較每個屬性,以組合其效果。

public class FlexiblePersonComparator implements Comparator<Person>
{
   private List<Comparator<Person>> comparators;

   public FlexiblePersonComparator(List<Comparator<Person>> comparators)
   {
      this.comparators = comparators;
   }

   @Override
   public int compare(Person p1, Person p2)
   {
      int comp;
      for (Comparator<Person> comparator : comparators)
      {
         comp = comparator.compare(p1, p2);
         if (comp != 0)
            return comp;
      }
      return 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
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM