写了一个比较器,实现功能如下:
有列表List list,list存放对象(如Person p等),对象里面有若干属性。
要求对列表按照列表对象的某个属性进行排序。列表对象类型未知,对象属性未知。
通过通知比较器列表对象类型和排序字段,以及升序或降序,来对列表进行排序
(其实就是实现复杂对象列表排序)
注:由于实际应用中,要排序的列表结果集已经得到,程序应用没有错误或异常,
比较器只实现排序功能,因此在比较器中封闭异常
本人实现代码如下,希望大家提供一些意见来完善
//比较器总接口类
package myutil.comparator;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Comparator;
public abstract class ListComparator implements Comparator{
public static final int DATATYPE_STRING=1;//字符串类型
public static final int DATATYPE_DOUBLE=2;//双精度浮点型
public static final int DATATYPE_DATE=3;//日期类型
public static final int ORDER_ASC=100;//顺序
public static final int ORDER_DESC=101;//倒序
protected String colName="";//被比较的属性名称
protected Class cl=null;//被比较的列表对象的类型
protected int order=ORDER_ASC;//顺序或倒序,默认为顺序
/**
* 比较器的比较方法,在子类中实现
*/
public abstract int compare(Object o1, Object o2);
/**
* 获取两个比较对象的指定属性(colName)的值
* @param obj1 要比较的一个对象
* @param obj2 要比较的另一个对象
* @return Object[],被比较的两个对象中指定属性的值的数组
*/
protected Object[] getValueObj(Object obj1,Object obj2){
if(obj1==null || obj2==null)
return null;
try {
int first=colName.charAt(0)-32;//获得字段名的首字母,并转化成大写
String mName="get"+(char)first+colName.substring(1);//获得字段的获取方法
Method m = cl.getMethod(mName,null);//构造方法对象
Object val1=m.invoke(obj1,null);//调用方法,得到对象1的colName属性值
Object val2=m.invoke(obj2,null);//调用方法,得到对象2的colName属性值
return new Object[]{val1,val2};//返回对象数组
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
/**
* 交换两个对象,用于控制升序或降序
* @param obj1
* @param obj2
* @return
*/
protected Object[] swap(Object obj1,Object obj2){
if(this.order==ORDER_DESC){
Object oTmp=obj1;
obj1=obj2;
obj2=oTmp;
}
return new Object[]{obj1,obj2};
}
/**
* @return 排序方式(顺序/倒序)
*/
public final int getOrder() {
return order;
}
/**
* @param 排序方式(顺序/倒序)
*/
public final void setOrder(int order) {
this.order = order;
}
/**
* @return 列表包装类型
*/
public final Class getCl() {
return cl;
}
/**
* @param 列表包装类型
*/
public final void setCl(Class cl) {
this.cl = cl;
}
/**
* @return 排序字段
*/
public final String getColName() {
return colName;
}
/**
* @param 排序字段
*/
public final void setColName(String colName) {
this.colName = colName;
}
public String toString(){
StringBuffer buf=new StringBuffer();
buf.append("Column Name(colName):").append(this.colName).append(";");
buf.append("Class In List Name(c1):").append(cl.getName()).append(";");
buf.append("Order Type(order):").append(this.order).append(".");
return buf.toString();
}
}
//工厂类
package myutil.comparator;
public class ListComparatorFactory {
public static final ListComparator getListComparator(int dataType){
switch(dataType){
case ListComparator.DATATYPE_STRING:
return new StringListComparator();
case ListComparator.DATATYPE_DOUBLE:
return new NumberListComparator();
case ListComparator.DATATYPE_DATE:
return new DateListComparator();
default:
return new DefaultListComparator();
}
}
}
//默认实现类
package myutil.comparator;
public class DefaultListComparator extends ListComparator{
public int compare(Object obj1, Object obj2) {
Object swapObj[]=this.swap(obj1,obj2);
Object obj[]=this.getValueObj(swapObj[0],swapObj[1]);
if(obj==null)
return 0;
Comparable cmp1=(Comparable)obj[0];
Comparable cmp2=(Comparable)obj[1];
return cmp1.compareTo(cmp2);
}
public String toString(){
return super.toString();
}
}
//比较字段为日期型的实现类
package myutil.comparator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class DateListComparator extends ListComparator{
public int compare(Object obj1, Object obj2) {
Object swapObj[]=this.swap(obj1,obj2);
Object obj[]=this.getValueObj(swapObj[0],swapObj[1]);
if(obj==null)
return 0;
DateFormat format=new SimpleDateFormat("yyyyMMddhhmmss");
Comparable cmp1,cmp2;
try{
cmp1=format.parse(obj[0]==null?"00000000000000":obj[0].toString());
}catch(ParseException pe){
cmp1=new Date(0);
}
try {
cmp2=format.parse(obj[1]==null?"00000000000000":obj[1].toString());
} catch (ParseException e) {
cmp2=new Date(0);
}
return cmp1.compareTo(cmp2);
}
public String toString(){
return super.toString();
}
}
//比较字段为数字型的实现类,兼容整数,浮点数
package myutil.comparator;
public class NumberListComparator extends ListComparator{
public int compare(Object obj1, Object obj2) {
Object swapObj[]=this.swap(obj1,obj2);
Object obj[]=this.getValueObj(swapObj[0],swapObj[1]);
if(obj==null)
return 0;
Comparable cmp1,cmp2;
try{
cmp1=new Double(obj[0]==null?"0":obj[0].toString());
}catch(NumberFormatException nfe){
cmp1=new Double(0);
}
try{
cmp2=new Double(obj[1]==null?"0":obj[1].toString());
}catch(NumberFormatException nfe){
cmp2=new Double(0);
}
return cmp1.compareTo(cmp2);
}
public String toString(){
return super.toString();
}
}
//比较字段为字符串型的实现类
package myutil.comparator;
public class StringListComparator extends ListComparator{
public int compare(Object obj1, Object obj2) {
Object swapObj[]=this.swap(obj1,obj2);
Object obj[]=this.getValueObj(swapObj[0],swapObj[1]);
if(obj==null)
return 0;
Comparable cmp1=obj[0]==null?"":obj[0].toString();
Comparable cmp2=obj[1]==null?"":obj[1].toString();
return cmp1.compareTo(cmp2);
}
public String toString(){
return super.toString();
}
}
//应用测试示例
package myutil.comparator.test;
public class MyFile {
private int age;
/**
* @return the age
*/
public final int getAge() {
return age;
}
/**
* @param age the age to set
*/
public final void setAge(int age) {
this.age = age;
}
public void display() {
// TODO Auto-generated method stub
}
public String toString(){
return String.valueOf(age);
}
}
package myutil.comparator.test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import myutil.comparator.ListComparator;
import myutil.comparator.ListComparatorFactory;
public class SortTest {
public List doIt(){
List list=new ArrayList(10);
Random ran=new Random();
for(int i=0;i<10;i++){
MyFile mf=new MyFile();
mf.setAge(ran.nextInt(100));
list.add(mf);
}
return list;
}
/**
* @param args
*/
public static void main(String[] args) {
SortTest t=new SortTest();
List list=t.doIt();
ListComparator cm=ListComparatorFactory.getListComparator(ListComparator.DATATYPE_DOUBLE);
cm.setColName("age");
cm.setCl(MyFile.class);
System.out.println(cm);
cm.setOrder(ListComparator.ORDER_DESC);
Collections.sort(list,cm);
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}