1. 程式人生 > >java類如何按照某一屬性排序

java類如何按照某一屬性排序

1. 首先Person類,實現可按照屬性name或者age排序,Personl類如下:

class Person{  
    public String name;  
    public  int age;  
      
    public Person(String name, int age) {  
        super();  
        this.name = name;  
        this.age = age;  
    }  
    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;  
    }  
    @Override  
    public String toString() {  
        return "Person [name=" + name + ", age=" + age + "]";  
    }  
}
2.首先看示例:利用Collections.sort方法實現對String,Integer...等等型別的排序

public class T1 {  
      
    public static void main(String[] args) {  
          
        List<String> list=new ArrayList<String>();  
        list.add("d");  
        list.add("f");  
        list.add("a");  
        sortString(list);  
          
        System.out.println("===========");  
          
        List<Integer> list2=new ArrayList<Integer>();  
        list2.add(9);  
        list2.add(5);  
        list2.add(8);  
        sortInteger(list2);  
    }  
      
    public static void sortString(List<String> list){  
        Collections.sort(list);  
          
        for (String string : list) {  
            System.out.println(string);  
        }  
    }  
    public static void sortInteger(List<Integer> list){  
        Collections.sort(list);  
          
        for (Integer integer : list) {  
            System.out.println(integer);  
        }  
    }  
} 

3.試試對Person類進行排序

public class T2 {  
      
    public static void main(String[] args) {  
          
        List<Person> list=new ArrayList<Person>();  
  
        Person p1=new Person("d",55);  
        Person p2=new Person("c",18);  
        Person p3=new Person("a",37);  
          
        list.add(p1);  
        list.add(p2);  
        list.add(p3);  
        sortPerson(list);  
    }  
      
    public static void sortPerson(List<Person> list){  
        Collections.sort(list);//編譯不通過!!!!  
    }  
}

編譯器提示"The method sort(List<T>) in the type Collections is not applicable for the arguments (List<Person>)"

要想使用Collections.sort進行排序 該class 必須實現Comparable<Person>介面,並覆寫compareTo方法;現在對Person類進行改造:

class Person implements Comparable<Person>{  
    public String name;  
    public  int age;  
      
    public Person(String name, int age) {  
        super();  
        this.name = name;  
        this.age = age;  
    }  
    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;  
    }  
    @Override  
    public String toString() {  
        return "Person [name=" + name + ", age=" + age + "]";  
    }  
    @Override  
    public int compareTo(Person o) {  
        return this.getName().compareTo(o.getName());  
    }  
}
再次測試:
public class T2 {  
      
    public static void main(String[] args) {  
          
        List<Person> list=new ArrayList<Person>();  
  
        Person p1=new Person("d",55);  
        Person p2=new Person("c",18);  
        Person p3=new Person("a",37);  
          
        list.add(p1);  
        list.add(p2);  
        list.add(p3);  
        sortPerson(list);  
    }  
      
    public static void sortPerson(List<Person> list){  
        Collections.sort(list);//編譯通過;  
        for (Person person : list) {  
            System.out.println(person);  
        }  
    }  
}
輸出結果:

Person [name=a, age=37]  
Person [name=c, age=18]  
Person [name=d, age=55]  

4.以上程式碼中實現了按照name屬性對List<Person>排序,如果想按照age屬性排序,需要修改>compareTo方法 如下:

   @Override  
    public int compareTo(Person o) {  
        return new Integer(this.getAge()).compareTo(o.getAge());  
    }

再次執行main方法測試結果如下:

Person [name=c, age=18]  
Person [name=a, age=37]  
Person [name=d, age=55]

5.其他實現方式(1):Person類可以不實現Comparable<T>介面,而是呼叫public static <T> void sort(List<T> list, Comparator<? super T> c)方法實現排序。


public static void sortPerson(List<Person> list){  
        Collections.sort(list,new Comparator<Person>() {  
            @Override  
            public int compare(Person o1, Person o2) {  
                return o1.getName().compareTo (o2.getName());  
            }  
        });  
          
        for (Person person : list) {  
            System.out.println(person);  
        }  
    }

6.其他實現方式(2):Person類可以不實現Comparable < T > 介面,而是通過自定義比較器類的方式實現排序。比較器程式碼如下:

// 自定義比較器:按name比較 
class NameComparator implements Comparator<Person> {  
  
    @Override  
    public int compare(Person o1, Person o2) {  
        return o1.getName().compareTo(o2.getName());    
    }    
}    
  
// 自定義比較器:按age比較    
class AgeComparator implements Comparator<Person> {    
    public int compare(Person object1, Person object2) {// 實現介面中的方法    
        return new Integer(object1.getAge()).compareTo(object2.getAge());  
    }    
} 
比較器使用方式如下: 
public class T2 {  
      
    public static void main(String[] args) {  
          
        List<Person> list=new ArrayList<Person>();  
  
        Person p1=new Person("d",55);  
        Person p2=new Person("c",18);  
        Person p3=new Person("a",37);  
          
        list.add(p1);  
        list.add(p2);  
        list.add(p3);  
        sortPerson(list);  
    }  
      
    public static void sortPerson(List<Person> list){  
          
        Collections.sort(list,new NameComparator());  
        for (Person person : list) {  
            System.out.println(person);  
        }  
    }  
}

執行結果如下:

Person [name=a, age=37]  
Person [name=c, age=18]  
Person [name=d, age=55]