來(lái)源 http://blog.sina.com.cn/s/blog_56fd58ab0100qdzb.html
當(dāng)需要排序的集合或數(shù)組不是單純的數(shù)字型時(shí)脱拼,通尘В可以使用Comparator或Comparable啄枕,以簡(jiǎn)單的方式實(shí)現(xiàn)對(duì)象排序或自定義排序。
一沪摄、Comparator
強(qiáng)行對(duì)某個(gè)對(duì)象collection進(jìn)行整體排序的比較函數(shù),可以將Comparator傳遞給Collections.sort或Arrays.sort沸版。
接口方法:
int compare(Object o1, Object o2);
案例:
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
強(qiáng)行對(duì)實(shí)現(xiàn)它的每個(gè)類的對(duì)象進(jìn)行整體排序兴蒸,實(shí)現(xiàn)此接口的對(duì)象列表(和數(shù)組)可以通過(guò)Collections.sort或Arrays.sort進(jìn)行自動(dòng)排序视粮。
接口方法:
int compareTo(Object o);
假設(shè)對(duì)象User,需要按年齡排序:
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;
}
}
改造后的對(duì)象:
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的區(qū)別
先看一下使用Comparator對(duì)User集合實(shí)現(xiàn)排序的方式:
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());
}
}
}
一個(gè)類實(shí)現(xiàn)了Camparable接口則表明這個(gè)類的對(duì)象之間是可以相互比較的蕾殴,這個(gè)類對(duì)象組成的集合就可以直接使用sort方法排序。Comparator可以看成一種算法的實(shí)現(xiàn)岛啸,將算法和數(shù)據(jù)分離钓觉,Comparator也可以在下面兩種環(huán)境下使用:1、類的設(shè)計(jì)師沒(méi)有考慮到比較問(wèn)題而沒(méi)有實(shí)現(xiàn)Comparable坚踩,可以通過(guò)Comparator來(lái)實(shí)現(xiàn)排序而不必改變對(duì)象本身2荡灾、可以使用多種排序標(biāo)準(zhǔn),比如升序瞬铸、降序等