用Collections.sort方法对list排序有两种方法
第一种是list中的对象实现Comparable接口,如下:
01 |
/** |
02 |
* |
03 |
*/ |
04 |
public class User implements Comparable
|
05 |
|
06 |
|
07 |
|
08 |
|
09 |
<user>
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
{
|
15 |
private String name;
|
16 |
private Integer order;
|
17 |
public String getName() {
|
18 |
return name;
|
19 |
}
|
20 |
public void setName(String name) {
|
21 |
this .name = name;
|
22 |
}
|
23 |
public Integer getOrder() {
|
24 |
return order;
|
25 |
}
|
26 |
public void setOrder(Integer order) {
|
27 |
this .order = order;
|
28 |
}
|
29 |
public int compareTo(User arg0) {
|
30 |
return this .getOrder().compareTo(arg0.getOrder());
|
31 |
}
|
32 |
} |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
</user>
|
测试一下:
01 |
public class Test{
|
02 |
03 |
public static void main(String[] args) {
|
04 |
User user1 = new User();
|
05 |
user1.setName( "a" );
|
06 |
user1.setOrder( 1 );
|
07 |
User user2 = new User();
|
08 |
user2.setName( "b" );
|
09 |
user2.setOrder( 2 );
|
10 |
List
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
<user>
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
list = new ArrayList
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
<user>
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
();
|
31 |
//此处add user2再add user1
|
32 |
list.add(user2);
|
33 |
list.add(user1);
|
34 |
Collections.sort(list);
|
35 |
for (User u : list){
|
36 |
System.out.println(u.getName());
|
37 |
}
|
38 |
}
|
39 |
} |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
</user>
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
</user>
|
输出结果如下
a
b
第二种方法是根据Collections.sort重载方法来实现,例如:
01 |
/** |
02 |
* |
03 |
*/ |
04 |
public class User { //此处无需实现Comparable接口
|
05 |
private String name;
|
06 |
private Integer order;
|
07 |
public String getName() {
|
08 |
return name;
|
09 |
}
|
10 |
public void setName(String name) {
|
11 |
this .name = name;
|
12 |
}
|
13 |
public Integer getOrder() {
|
14 |
return order;
|
15 |
}
|
16 |
public void setOrder(Integer order) {
|
17 |
this .order = order;
|
18 |
}
|
19 |
} |
主类中这样写即可:
01 |
public class Test{
|
02 |
public static void main(String[] args) {
|
03 |
User user1 = new User();
|
04 |
user1.setName( "a" );
|
05 |
user1.setOrder( 1 );
|
06 |
User user2 = new User();
|
07 |
user2.setName( "b" );
|
08 |
user2.setOrder( 2 );
|
09 |
List
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
<user>
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
list = new ArrayList
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
<user>
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
();
|
30 |
list.add(user2);
|
31 |
list.add(user1);
|
32 |
|
33 |
Collections.sort(list, new Comparator
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
<user>
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
(){
|
44 |
public int compare(User arg0, User arg1) {
|
45 |
return arg0.getOrder().compareTo(arg1.getOrder());
|
46 |
}
|
47 |
});
|
48 |
for (User u : list){
|
49 |
System.out.println(u.getName());
|
50 |
}
|
51 |
}
|
52 |
} |
53 |
|
54 |
|
55 |
|
56 |
|
57 |
</user>
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
</user>
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
</user>
|
输出结果如下
a
b
前者代码结构简单,但是只能根据固定的属性排序,后者灵活,可以临时指定排序项,但是代码不够简洁
多字段的场合:
01 |
Collections.sort(list, new Comparator
|
02 |
|
03 |
|
04 |
|
05 |
|
06 |
<user>
|
07 |
|
08 |
|
09 |
|
10 |
|
11 |
(){
|
12 |
public int compare(User arg0, User arg1) {
|
13 |
// 第一次比较专业
|
14 |
int i = arg0.getOrder().compareTo(arg1.getOrder());
|
15 |
16 |
// |
17 |
if (i== 0 ){
|
18 |
// |
19 |
int j=arg0.getXXX().compareTo(arg1.getXXX());
|
20 |
// |
21 |
if (j== 0 ){
|
22 |
return arg0.getCCC().compareTo(arg1.getCCC());
|
23 |
}
|
24 |
return j;
|
25 |
}
|
26 |
return i;
|
27 |
}
|
28 |
});
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
</user>
|