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

简介:

有一个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++构造函数在数组中的使用
55 0
|
3天前
|
JavaScript
类数组是什么
类数组是什么
6 0
|
4天前
|
JavaScript 前端开发 索引
往数组添加对象的方法
往数组添加对象的方法
10 0
|
6月前
|
存储 机器学习/深度学习 Java
Java数组的定义与使用
Java数组的定义与使用
52 0
|
6月前
|
存储 算法 C语言
27 C++ - 对象成员变量和函数的存储
27 C++ - 对象成员变量和函数的存储
21 0
|
11月前
对象定义-解构-枚举属性遍历以及对象内函数
对象定义-解构-枚举属性遍历以及对象内函数
52 0
|
存储
2-成员变量和成员函数分开存储
2-成员变量和成员函数分开存储
【C++之对象数组和对象指针1】输出第1,3,5个学生的数据
【C++之对象数组和对象指针1】输出第1,3,5个学生的数据
|
编译器 C++
C++类对象构造顺序
C++类对象构造顺序
40 0
C++类对象构造顺序
|
安全 编译器 C++
【C++要笑着学】类的默认成员函数详解 | 构造函数 | 析构函数 | 构造拷贝函数(二)
朋友们好啊,今天终于更新了。我是柠檬叶子C,本章将继续讲解C++中的面向对象的知识点,本篇主要讲解默认成员函数中的构造函数、析构函数和拷贝构造函数。还是和以前一样,我们将由浅入深地去讲解,以 "初学者" 的角度去探索式地学习。会一步步地推进讲解,而不是直接把枯燥的知识点倒出来,应该会有不错的阅读体验。如果觉得不错,可以 "一键三连" 支持一下博主!你们的关注就是我更新的最大动力!Thanks ♪ (・ω・)ノ
74 0
【C++要笑着学】类的默认成员函数详解 | 构造函数 | 析构函数 | 构造拷贝函数(二)