當需要排序的集合或數組不是單純的數字型時,通??梢允褂肅omparator或Comparable,以簡單的方式實現對象排序或自定義排序。
A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that don't have a natural ordering. ------API
對字符串List可以直接sort進行排序, 那是因為String 這個對象已經幫我們實現了 Comparable接口 , 所以我們的 Person 如果想排序, 也要實現一個比較器。
一. Comparator
對Linkedlist存儲的對象進行排序
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
|
import java.util.Comparator; import java.util.LinkedList; class Person{ private float height; private String name; Person( float height) { this .height=height; } public float getHeight() { return height; } public void setHeight( float height) { this .height = height; } public String getName() { return name; } public void setName(String name) { this .name = name; } } class PersonHeight implements Comparator<Person>{ @Override //重寫compare方法,return<0不變,return>0則交換順序(保持升序) public int compare(Person e1, Person e2) { if (e1.getHeight() < e2.getHeight()){ return 1 ; } else { return - 1 ; } } } public class Question3 { public static void main(String[] args) { Person p1= new Person( 23 .4f); p1.setName( "Stud1" ); Person p2= new Person( 2 .34f); p2.setName( "Stud2" ); Person p3= new Person( 34 .32f); p3.setName( "Stud3" ); Person p4= new Person( 56 .45f); p4.setName( "Stud4" ); Person p5= new Person( 21 .4f); p5.setName( "Stud5" ); LinkedList<Person> al= new LinkedList<Person>(); al.add(p1); al.add(p2); al.add(p3); al.add(p4); al.add(p5); //調用sort方法,實現排序 Collections.sort(al, new PersonHeight()); //遍歷輸出 for (Person p:al) System.out.println(p.getName()); } } |
附加:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//對日期進行排序 /** * 如果o1小于o2,返回一個負數;如果o1大于o2,返回一個正數;如果他們相等,則返回0; */ @Override public int compare(Step o1, Step o2) { Date acceptTime1=UtilTool.strToDate(o1.getAcceptTime(), null ); Date acceptTime2=UtilTool.strToDate(o2.getAcceptTime(), null ); //對日期字段進行升序,如果欲降序可采用before方法 if (acceptTime1.after(acceptTime2)) return 1 ; return - 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
51
52
53
54
55
56
57
58
59
|
import java.util.Collections; import java.util.Comparator; import java.util.LinkedList; class Person implements Comparable{ private float height; private String name; Person( float height) { this .height=height; } public float getHeight() { return height; } public void setHeight( float height) { this .height = height; } public String getName() { return name; } public void setName(String name) { this .name = name; } @Override public int compareTo(Object o) { // TODO Auto-generated method stub if ( this .height>((Person)o).height){ return 1 ; } else return - 1 ; } } public class Question3 { public static void main(String[] args) { Person p1= new Person( 23 .4f); p1.setName( "Stud1" ); Person p2= new Person( 2 .34f); p2.setName( "Stud2" ); Person p3= new Person( 34 .32f); p3.setName( "Stud3" ); Person p4= new Person( 56 .45f); p4.setName( "Stud4" ); Person p5= new Person( 21 .4f); p5.setName( "Stud5" ); LinkedList<Person> al= new LinkedList<Person>(); al.add(p1); al.add(p2); al.add(p3); al.add(p4); al.add(p5); Collections.sort(al); for (Person p:al) System.out.println(p.getName()); } } |
三.比較
Comparable 定義在 Person類的內部。
Comparator 是定義在Person的外部的, 此時我們的Person類的結構不需要有任何變化。
兩種方法各有優劣, 用Comparable 簡單, 只要實現Comparable 接口的對象直接就成為一個可以比較的對象,但是需要修改源代碼, 用Comparator 的好處是不需要修改源代碼, 而是另外實現一個比較器, 當某個自定義的對象需要作比較的時候,把比較器和對象一起傳遞過去就可以比大小了, 并且在Comparator 里面用戶可以自己實現復雜的可以通用的邏輯,使其可以匹配一些比較簡單的對象,那樣就可以節省很多重復勞動了。