按照指定对象的成员变量排序

简介:

有一个List<CommonDictionary>,

CommonDictionary的结构:

Java代码   收藏代码
  1. /** 
  2.      * 主键id 
  3.      */  
  4.     private Long id;  
  5.     /** 
  6.      * 组id 
  7.      */  
  8.     private String groupId;  
  9.     /** 
  10.      * 键<br />不能取值为key,因为key是关键字 
  11.      */  
  12.     private String key2;  
  13.     /** 
  14.      * 值 
  15.      */  
  16.     private String value;  
  17.     /** 
  18.      * 描述 
  19.      */  
  20.     private String description;  

 

 

我需要对key2进行排序.

说明:

(1)key2的值是唯一的,不重复;

(2)key2的所有取值都已知.

按照什么方式排序呢?

Java代码   收藏代码
  1. "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

---------------------------------

排序后:

13company_name武汉市

14product_standard产品标准aa

15production_no生产证号

16registration_certificate_number注册证号drfdf

18production_address生产地址

17company_website公司网址

测试方法如下:

Java代码   收藏代码
  1. @Test  
  2.     public void tst_77(){  
  3.         List<CommonDictionary> list=DictionaryParam.getList(Constant2.DICTIONARY_GROUP_ANTICOUNTERFEIT_CODE);  
  4.         String orderTitles[]=new String[]{"company_n","product_stand","production_no","registration_c","production_addr","company_web"};  
  5.           
  6.         for(int i=0;i<list.size();i++){  
  7.             CommonDictionary commonDictionary33=list.get(i);  
  8.             System.out.println(commonDictionary33.getId()+"\t"+commonDictionary33.getKey2()+"\t"+commonDictionary33.getValue());  
  9.         }  
  10.           
  11.         Collections.sort(list,new SystemHWUtil. ArrayListComparator(orderTitles,"Key2"));  
  12.         System.out.println("---------------------------------");  
  13.         for(int i=0;i<list.size();i++){  
  14.             CommonDictionary commonDictionary33=list.get(i);  
  15.             System.out.println(commonDictionary33.getId()+"\t"+commonDictionary33.getKey2()+"\t"+commonDictionary33.getValue());  
  16.         }  
  17.     }  

 执行结果:

 

SystemHWUtil. ArrayListComparator 实现如下:

Java代码   收藏代码
  1. public static class ArrayListComparator implements Comparator{  
  2.         /*** 
  3.          * 排序的依据 
  4.          */  
  5.         private String titles[];  
  6.         /*** 
  7.          * 对哪个列进行排序 
  8.          */  
  9.         private String comparedProperty;  
  10.           
  11.         public ArrayListComparator(String[] titles,String comparedProperty) {  
  12.             super();  
  13.             this.titles = titles;  
  14.             this.comparedProperty=comparedProperty;  
  15.         }  
  16.   
  17.         public int compare(Object o1, Object o2) {  
  18.             if(null!=o1&&null!=o2)  
  19.             {  
  20.                   
  21.                 try {  
  22.                     if(SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o1, comparedProperty)   ) >  
  23.                     SystemHWUtil.indexOfArr(titles,(String)ReflectHWUtils.getObjectValue(o2, comparedProperty))){  
  24.                         return 1/*大于*/;  
  25.                     }else {  
  26.                         return -1/*小于*/;  
  27.                     }  
  28.                 } catch (SecurityException e) {  
  29.                     e.printStackTrace();  
  30.                 } catch (NoSuchFieldException e) {  
  31.                     e.printStackTrace();  
  32.                 } catch (IllegalArgumentException e) {  
  33.                     e.printStackTrace();  
  34.                 } catch (IllegalAccessException e) {  
  35.                     e.printStackTrace();  
  36.                 }  
  37.             }  
  38.             return 0/*等于*/;  
  39.         }  
  40.           
  41.     }  

 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};

相关文章
|
7月前
|
编译器 C++ 开发者
C++构造函数在数组中的使用
C++构造函数在数组中的使用
53 0
|
6月前
|
存储 算法 C语言
27 C++ - 对象成员变量和函数的存储
27 C++ - 对象成员变量和函数的存储
20 0
|
10月前
根据数组中对象的属性值进行排序
根据数组中对象的属性值进行排序
64 0
|
11月前
对象定义-解构-枚举属性遍历以及对象内函数
对象定义-解构-枚举属性遍历以及对象内函数
48 0
|
编译器 C++
C++类对象构造顺序
C++类对象构造顺序
40 0
C++类对象构造顺序
|
Java 数据库
java反射机制查找类的属性并赋值
先说一下需求:最近做一个项目其中需要将前台传来的数组存到数据库,但是这个表里有15个字段,集合是不固定的,然后要把这个集合的数值赋给这个类的相应属性,然后存到数据库中。集合长度应小于等于这个类属性的个数。
276 0
类初始列表和类对象作为类成员
类初始列表和类对象作为类成员
类初始列表和类对象作为类成员
|
存储
成员变量和方法的区别?
成员变量有两种: 实例变量 类变量(也称静态变量,静态域)
130 0
|
Java
构造器、对象数组、对象属性、静态实例块、this关键字
它的名字:构造函数、构造方法、初始化方法。 构造条件? 以后我们如何去使用构造器? 你觉得那些对象中那些属性值是必要的,你就在构造器中提出来(就是你如果想创造对象必须要传入我所需要的参数) public class Dog {//这里就是一个简单的类、狗的类 String name; .
1011 0