JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排序

JAVA中Arrays.sort()使用两种方式(Comparable和Comparator接口)对对象或者引用进行排

这里我们采用两种方式,一种是使用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(待排序对象,该排序方式类的对象)

数组的length是一个属性,而字符串的length()是一个方法了!!!虽然都是求的他们各自的长度( 与此题无关 )。

Sorting an Array :

      1. 数字排序  int[] intArray = new int[] { 4, 1, 3, -23 };

  Arrays.sort(intArray);

  输出: [-23, 1, 3, 4]

  2. 字符串排序,先大写后小写 String[] strArray = new String[] { "z", "a", "C" };

  Arrays.sort(strArray);

  输出: [C, a, z]

  3. 严格按字母表顺序排序,也就是忽略大小写排序 Case-insensitive sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  输出: [a, C, z]

  4. 反向排序, Reverse-order sort

  Arrays.sort(strArray, Collections.reverseOrder());

  输出:[z, a, C]

  5. 忽略大小写反向排序 Case-insensitive reverse-order sort

  Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);

  Collections.reverse(Arrays.asList(strArray));

  输出: [z, C, a]

  java初学者最常见的错误思想,就是试图去写一些方法来完成数组的排序功能,其实,数组排序功能,在java的api里面早已实现,我们没有 必要去重复制造轮子。


二、源代码 (两种方式(Comparable和Comparator接口)对对象或者引用进行排序)

方式1:使用Comparable接口

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package tong.day4_27.systemUse;
 
import java.util.Arrays;
/**
  * 使用Comparable接口:让待排序对象所在的类实现Comparable接口,并重写Comparable接口中的compareTo()方法
  * 缺点是只能按照一种规则排序
  * @author tong
  *
  */
public class ObjectSort {
 
     public static void main(String[] args) {
         Person[] persons = new Person[ 5 ];
         persons[ 0 ] = new Person( "tom" , 45 );
         persons[ 1 ] = new Person( "jack" , 12 );
         persons[ 2 ] = new Person( "bill" , 21 );
         persons[ 3 ] = new Person( "kandy" , 34 );
         persons[ 4 ] = new Person();
         Arrays.sort(persons);
         for (Person person:persons) {
             System.out.println(person);
         }
 
     }
 
}
class Person implements Comparable<person>{
     private String name;
     private int age;
     
     public Person(String name, int age){
         this .name = name;
         this .age = age;
     }
     public Person(){
         this ( "unknown" , 0 );
     }
     //重写该类的compareTo()方法,使其按照从小到大顺序排序
     @Override
     public int compareTo(Person o) {
          return age-o.age;
         
     }
     //重写Student类的toString()方法,在输入对象时按照以下方式输出
     @Override
     public String toString() {     
         return "Person[name:" +name+ ",age:" +age+ "]" ;
     }
     
}</person>
运行结果:

 

comparable

 

方式2:使用Comparator接口

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package tong.day4_27.systemUse;
 
import java.util.Arrays;
import java.util.Comparator;
 
/**
  * 使用Comparator接口:编写多个排序方式类实现Comparator接口,并重写新Comparator接口中的compare()方法
  * public static <t> void sort(T[] a,Comparator<!--? super T--> c),根据指定比较器产生的顺序对指定对象数组进行排序。数组中的所有元素都必须是通过指定比较器可相互比较的
  * (也就是说,对于数组中的任何 e1 和 e2 元素而言,c.compare(e1, e2) 不得抛出 ClassCastException)。
  * 优点是可以按照多种方式排序,你要按照什么方式排序,就创建一个实现Comparator接口的排序方式类,然后将该排序类的对象传入到Arrays.sort(待排序对象,该排序方式类的对象)
  * @author tong
  *
  */
 
public class ComparatorUse {
 
     
     public static void main(String[] args) {
         Student[] persons = new Student[ 5 ];
         persons[ 0 ] = new Student( "tom" , 1 , 88 , 45 );
         persons[ 1 ] = new Student( "jack" , 6 , 80 , 12 );
         persons[ 2 ] = new Student( "bill" , 4 , 68 , 21 );
         persons[ 3 ] = new Student( "kandy" , 2 , 98 , 34 );
         persons[ 4 ] = new Student( "lily" , 5 , 94 , 20 );
         System.out.println( "排序前的数据:" );
         for (Student student:persons) {
             System.out.println(student);
         }
         //创建SortByNumber对象,将其作为参数传入Arrays.sort(persons,sortByNumber)方法中
         SortByNumber sortByNumber = new SortByNumber();
         Arrays.sort(persons,sortByNumber);
         System.out.println( "根据学生编号由低到高排序:" );
         for (Student student:persons) {
             System.out.println(student);
         }
         SortByScore sortByScore = new SortByScore();
         Arrays.sort(persons,sortByScore);
         System.out.println( "根据学生成绩由高到低排序:" );
         for (Student student:persons) {
             System.out.println(student);
         }
 
 
     }
 
}
 
class Student {
     private String name;
     private int number;
     private int score;
     private int age;
     
     public Student(String name, int number, int score, int age){
         this .name = name;
         this .number = number;
         this .score = score;
         this .age = age;
     }
     //重写Student类的toString()方法,在输入对象时按照以下方式输出
     @Override
     public String toString() {     
         return "Student[name:" +name+ ",age:" +age+ ",number:" +number+ ",score:" +score+ "]" ;
     }
 
     public String getName() {
         return name;
     }
 
     public void setName(String name) {
         this .name = name;
     }
 
     public int getNumber() {
         return number;
     }
 
     public void setNumber( int number) {
         this .number = number;
     }
 
     public int getScore() {
         return score;
     }
 
     public void setScore( int score) {
         this .score = score;
     }
 
     public int getAge() {
         return age;
     }
 
     public void setAge( int age) {
         this .age = age;
     }
     
     
     
}
//按照学号由低到高排列,创建SortByNumber类,该类实现Comparator,重写该接口的compare()
class SortByNumber implements Comparator<student>{
     //重写该接口的compare()使其按照学号由小到大排序(前者减去后者)
     @Override
     public int compare(Student o1, Student o2) {
         return o1.getNumber()-o2.getNumber();
         
     }
     
}
//按照分数由高到低排列,创建SortByScore类,该类实现Comparator,重写该接口的compare()
class SortByScore implements Comparator<student>{
     //重写该接口的compare()使其按照分数由高到低排序(后者减去前者)
     @Override
     public int compare(Student o1, Student o2) {
         return o2.getScore()-o1.getScore();
         
     }
     
}</student></student></t>
运行结果:

 

加载中...vcg==" src="http://img.2cto.com/Collfiles/20150428/20150428084639195.png" />


  • 6
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值