有一个list<commondictionary>,
commondictionary的结构:

/**
* 主键id
*/
private long id;
/**
* 组id
private string groupid;
* 键<br />不能取值为key,因为key是关键字
private string key2;
* 值
private string value;
* 描述
private string description;
我需要对key2进行排序.
说明:
(1)key2的值是唯一的,不重复;
(2)key2的所有取值都已知.
按照什么方式排序呢?

"company_n","product_stand","production_no","registration_c","production_addr","company_web"
排序前:
13company_name武汉市
17company_website公司网址
14product_standard产品标准aa
18production_address生产地址
15production_no生产证号
16registration_certificate_number注册证号drfdf
---------------------------------
排序后:
测试方法如下:

@test
public void tst_77(){
list<commondictionary> list=dictionaryparam.getlist(constant2.dictionary_group_anticounterfeit_code);
string ordertitles[]=new string[]{"company_n","product_stand","production_no","registration_c","production_addr","company_web"};
for(int i=0;i<list.size();i++){
commondictionary commondictionary33=list.get(i);
system.out.println(commondictionary33.getid()+"\t"+commondictionary33.getkey2()+"\t"+commondictionary33.getvalue());
}
collections.sort(list,new systemhwutil. arraylistcomparator(ordertitles,"key2"));
system.out.println("---------------------------------");
}
执行结果:
systemhwutil. arraylistcomparator 实现如下:

public static class arraylistcomparator implements comparator{
/***
* 排序的依据
*/
private string titles[];
* 对哪个列进行排序
private string comparedproperty;
public arraylistcomparator(string[] titles,string comparedproperty) {
super();
this.titles = titles;
this.comparedproperty=comparedproperty;
public int compare(object o1, object o2) {
if(null!=o1&&null!=o2)
{
try {
if(systemhwutil.indexofarr(titles,(string)reflecthwutils.getobjectvalue(o1, comparedproperty) ) >
systemhwutil.indexofarr(titles,(string)reflecthwutils.getobjectvalue(o2, comparedproperty))){
return 1/*大于*/;
}else {
return -1/*小于*/;
}
} catch (securityexception e) {
e.printstacktrace();
} catch (nosuchfieldexception e) {
} catch (illegalargumentexception e) {
} catch (illegalaccessexception e) {
}
}
return 0/*等于*/;
systemhwutil.indexofarr 参考http://hw1287789687.iteye.com/blog/2145187
注意:
(1)titles中的元素不要有重复;
(2)arraylistcomparator 可应用于所有的object,应该它没有与具体的类关联,而是通过反射来获取成员变量的值.
关于反射,可以参阅:http://hw1287789687.iteye.com/blog/2124280
遗留问题:
如何高效率地过滤string[]:{1,2,3,2}-->{1,2,3};