直接上代码
package com.galaxy.fym.model;
import java.util.Comparator;
/**
* Created by fengyiming on 2016/7/25.
*/
public class FymComparator{
public Integer key;
public String value;
public FymComparator(){
}
public FymComparator(Integer key,String value){
this.key = key;
this.value = value;
}
/**
* 定义了一个比较的方法
*/
public int compare(FymComparator obj2){
if(this.key == null){
return -1;
}else if(obj2.getKey() == null){
return 1;
}else{
int compareKeyValue = this.key.compareTo(obj2.getKey());
if(compareKeyValue == 0){
return this.value.compareTo(obj2.getValue());
}
return compareKeyValue;
}
}
/**
* 定义了一个比较器
*/
public static Comparator<FymComparator> comparable = new Comparator<FymComparator>() {
public int compare(FymComparator o1,FymComparator o2) {
return o1.compare(o2);
}
};
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package com.galaxy.fym.model;
/**
* Created by fengyiming on 2016/7/25.
* 千万要继承Comparable<Object>,不然实现的时候无法把类当参数,参数类型为默认的Object
*/
public class FymCompareble implements Comparable<FymCompareble>{
public Integer key;
public String value;
public FymCompareble(){
}
public FymCompareble(Integer key,String value){
this.key = key;
this.value = value;
}
public Integer getKey() {
return key;
}
public void setKey(Integer key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
/**
* 定义了一个比较的方法
*/
public int compare(FymCompareble obj2){
if(this.key == null){
return -1;
}else if(obj2.getKey() == null){
return 1;
}else{
int compareKeyValue = this.key.compareTo(obj2.getKey());
if(compareKeyValue == 0){
return this.value.compareTo(obj2.getValue());
}
return compareKeyValue;
}
}
/**
* Collections.sort()会自动调用这个方法排序
* @param o
* @return
*/
public int compareTo(FymCompareble o) {
return this.compare(o);
}
}
测试类:
package com.galaxy.fym.test;
import com.galaxy.fym.model.FymComparator;
import com.galaxy.fym.model.FymCompareble;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
/**
* Created by fengyiming on 2016/7/25.
*/
public class FymTest {
private final static Integer SIZE = 10;
private List<FymCompareble> comparebleList = new ArrayList<FymCompareble>(SIZE);
private List<FymComparator> comparatorList = new ArrayList<FymComparator>(SIZE);
private Random random = new Random();
@Before
public void init() {
for (int i = 0; i < SIZE; i++) {
int randomIndex = random.nextInt(SIZE);
FymCompareble fymCompareble = new FymCompareble(SIZE - randomIndex, "index" + i);
FymComparator fymComparator = new FymComparator(randomIndex, "index" + i);
comparebleList.add(fymCompareble);
comparatorList.add(fymComparator);
}
for (FymCompareble dto : comparebleList) {
System.out.println("comparebleList :" + dto.getKey() + "--" + dto.getValue());
}
System.out.println("---------------------------------------------------------");
for (FymComparator dto : comparatorList) {
System.out.println("comparatorList :" + dto.getKey() + "--" + dto.getValue());
}
System.out.println("--------------------------sort before-------------------------------");
}
@Test
public void printSort() {
Collections.sort(comparebleList);
Collections.sort(comparatorList, FymComparator.comparable);
}
@After
public void print() {
System.out.println("--------------------------sort after-------------------------------");
for (FymCompareble dto : comparebleList) {
System.out.println("comparebleList :" + dto.getKey() + "--" + dto.getValue());
}
System.out.println("---------------------------------------------------------------------------");
for (FymComparator dto : comparatorList) {
System.out.println("comparatorList :" + dto.getKey() + "--" + dto.getValue());
}
}
}
输出结果
comparebleList :6--index0
comparebleList :5--index1
comparebleList :6--index2
comparebleList :2--index3
comparebleList :9--index4
comparebleList :9--index5
comparebleList :2--index6
comparebleList :8--index7
comparebleList :1--index8
comparebleList :9--index9
---------------------------------------------------------
comparatorList :4--index0
comparatorList :5--index1
comparatorList :4--index2
comparatorList :8--index3
comparatorList :1--index4
comparatorList :1--index5
comparatorList :8--index6
comparatorList :2--index7
comparatorList :9--index8
comparatorList :1--index9
--------------------------sort before-------------------------------
--------------------------sort after-------------------------------
comparebleList :1--index8
comparebleList :2--index3
comparebleList :2--index6
comparebleList :5--index1
comparebleList :6--index0
comparebleList :6--index2
comparebleList :8--index7
comparebleList :9--index4
comparebleList :9--index5
comparebleList :9--index9
---------------------------------------------------------------------------
comparatorList :1--index4
comparatorList :1--index5
comparatorList :1--index9
comparatorList :2--index7
comparatorList :4--index0
comparatorList :4--index2
comparatorList :5--index1
comparatorList :8--index3
comparatorList :8--index6
comparatorList :9--index8
Process finished with exit code 0