码迷,mamicode.com
首页 > 编程语言 > 详细

31、Arrays数组排序(续)——自定义排序

时间:2016-05-26 18:50:14      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

  自定义的类要按照一定的方式进行排序,比如一个Person类要按照年龄进行从小到大排序,比如一个Student类要按照成绩进行由高到低排序。

  这里我们采用两种方式,一种是使用Comparable接口:让待排序对象所在的类实现Comparable接口,并重写Comparable接口中的compareTo()方法,缺点是只能按照一种规则排序。

  另一种方式是使用Comparator接口:编写多个排序方式类实现Comparator接口,并重写新Comparator接口中的compare()方法,在调用Arrays的sort()时将排序类对象作为参数传入:public static void sort(T[] a,Comparatorc),根据指定比较器产生的顺序对指定对象数组进行排序。数组中的所有元素都必须是通过指定比较器可相互比较的(也就是说,对于数组中的任何 e1 和 e2 元素而言,c.compare(e1, e2) 不得抛出 ClassCastException)。优点是可以按照多种方式排序,你要按照什么方式排序,就创建一个实现Comparator接口的排序方式类,然后将该排序类的对象传入到Arrays.sort(待排序对象,该排序方式类的对象)

 

方式一:Comparable

   

 1 import java.util.Arrays;
 2 
 3 
 4 public class Hello {
 5     public static void main(String[] args) {
 6         Person p1=new Person("p1",25);
 7         Person p2=new Person("p2",23);
 8         Person p3=new Person("p3",27);
 9         Person p4=new Person("p4",32);
10         Person p5=new Person("p5",18);
11         
12         Person[] arr=new Person[]{p1,p2,p3,p4,p5};
13         System.out.println(Arrays.toString(arr));
14         Arrays.sort(arr);
15         System.out.println(Arrays.toString(arr));
16     }
17 }
18 class Person implements Comparable<Person>{
19     private String name;
20     private int age;
21     public Person(String name, int age) {
22         this.name = name;
23         this.age = age;
24     }
25     @Override
26     public int compareTo(Person o) {
27         return age-o.age;
28     }
29     @Override
30     public String toString() {
31         return "Person [name=" + name + ", age=" + age + "]";
32     }
33 }

输出结果:

[Person [name=p1, age=25], Person [name=p2, age=23], Person [name=p3, age=27], Person [name=p4, age=32], Person [name=p5, age=18]]
[Person [name=p5, age=18], Person [name=p2, age=23], Person [name=p1, age=25], Person [name=p3, age=27], Person [name=p4, age=32]]

 

方式二:Comparator

 1 import java.util.Arrays;
 2 import java.util.Comparator;
 3 
 4 
 5 public class Hello {
 6     public static void main(String[] args) {
 7         Student s1=new Student("s1", 23, 89);
 8         Student s2=new Student("s2", 33, 68);
 9         Student s3=new Student("s3", 31, 75);
10         Student s4=new Student("s4", 17, 80);
11         Student[] arr=new Student[]{s1,s2,s3,s4};
12         
13         System.out.println("未排序:"+Arrays.toString(arr));
14         Arrays.sort(arr, new SortByAge());
15         System.out.println("按年龄排序:"+Arrays.toString(arr));
16         Arrays.sort(arr, new SortByScore());
17         System.out.println("按分数排序:"+Arrays.toString(arr));
18     }
19 }
20 class Student {
21     private String name;
22     private int age;
23     private double score;
24     public String getName() {
25         return name;
26     }
27     public void setName(String name) {
28         this.name = name;
29     }
30     public int getAge() {
31         return age;
32     }
33     public void setAge(int age) {
34         this.age = age;
35     }
36     public double getScore() {
37         return score;
38     }
39     public void setScore(double score) {
40         this.score = score;
41     }
42     public Student(String name, int age, double score) {
43         super();
44         this.name = name;
45         this.age = age;
46         this.score = score;
47     }
48     @Override
49     public String toString() {
50         return "[name=" + name + ", age=" + age + ", score=" + score
51                 + "]";
52     }
53     
54 }
55 
56 class SortByAge implements Comparator<Student>{
57 
58     @Override
59     public int compare(Student o1, Student o2) {
60         return o1.getAge()-o2.getAge();
61     }
62 }
63 
64 class SortByScore implements Comparator<Student>{
65 
66     @Override
67     public int compare(Student o1, Student o2) {
68         // TODO Auto-generated method stub
69         return  o1.getScore()-o2.getScore()>0?1:-1;
70     }
71 }

输出结果:

未排序:[[name=s1, age=23, score=89.0], [name=s2, age=33, score=68.0], [name=s3, age=31, score=75.0], [name=s4, age=17, score=80.0]]
按年龄排序:[[name=s4, age=17, score=80.0], [name=s1, age=23, score=89.0], [name=s3, age=31, score=75.0], [name=s2, age=33, score=68.0]]
按分数排序:[[name=s2, age=33, score=68.0], [name=s3, age=31, score=75.0], [name=s4, age=17, score=80.0], [name=s1, age=23, score=89.0]]

 

对于临时使用的可以使用匿名类的方式:(按年龄降序排序)

Arrays.sort(arr,new Comparator<Student>(){
    @Override
    public int compare(Student o1, Student o2) {
        // TODO Auto-generated method stub
        return o2.getAge()-o1.getAge();
    }    
});    

 

31、Arrays数组排序(续)——自定义排序

标签:

原文地址:http://www.cnblogs.com/caoyc/p/5532133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!