java对象排序-Comparator和Comparable

当需要排序的集合或数组不是单纯的数字型时,通常可以使用Comparator或Comparable,以简单的方式实现对象排序或自定义排序。

一、Comparator

强行对某个对象collection进行整体排序的比较函数,可以将Comparator传递给Collections.sort或Arrays.sort。
接口方法:int compare(Object o1, Object o2);
举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Arrays;
import java.util.Comparator;
public class SampleComparator implements Comparator {
public int compare(Object o1, Object o2) {
return toInt(o1) - toInt(o2);
}
private int toInt(Object o) {
String str = (String) o;
str = str.replaceAll("一", "1");
str = str.replaceAll("二", "2");
str = str.replaceAll("三", "3");
//
return Integer.parseInt(str);
}
public static void main(String[] args) {
String[] array = new String[] { "一二", "三", "二" };
Arrays.sort(array, new SampleComparator());
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}

二、Comparable

强行对实现它的每个类的对象进行整体排序,实现此接口的对象列表(和数组)可以通过Collections.sort或Arrays.sort进行自动排序。
接口方法: int compareTo(Object o);
假设对象User,需要按年龄排序:

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
public class User {

private String id;
private int age;

public User(String id, int age) {
this.id = id;
this.age = age;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

}

改造后的对象:

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
import java.util.Arrays;

public class User implements Comparable {

private String id;
private int age;

public User(String id, int age) {
this.id = id;
this.age = age;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public int compareTo(Object o) {
return this.age - ((User) o).getAge();
}


public static void main(String[] args) {
User[] users = new User[] { new User("a", 30), new User("b", 20) };
Arrays.sort(users);
for (int i = 0; i < users.length; i++) {
User user = users[i];
System.out.println(user.getId() + " " + user.getAge());
}
}

}

三、Comparator和Comparable的区别

先看一下使用Comparator对User集合实现排序的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Arrays;
import java.util.Comparator;

public class UserComparator implements Comparator {

public int compare(Object o1, Object o2) {
return ((User) o1).getAge() - ((User) o2).getAge();
}
public static void main(String[] args) {
User[] users = new User[] { new User("a", 30), new User("b", 20) };
Arrays.sort(users, new UserComparator());
for (int i = 0; i < users.length; i++) {
User user = users[i];
System.out.println(user.getId() + " " + user.getAge());
}
}
}

一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序。Comparator可以看成一种算法的实现,将算法和数据分离,Comparator也可以在下面两种环境下使用:

  1. 类的设计师没有考虑到比较问题而没有实现Comparable,可以通过Comparator来实现排序而不必改变对象本身
  2. 可以使用多种排序标准,比如升序、降序等

四、补充

一个例子:

1
2
3
4
5
6
7
8
List<Person> persons = new ArrayList<Person>();
Collections.sort(persons, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(o1.getName(), o2.getName());
}
});

使用Collections.sort方法,传一个Comparator实现类,比较对象字段实现compare方法。

References

  1. java对象排序-Comparator和Comparable
  2. Java根据对象的某个字段排序